如何配置Hibernate Lucene的ReadApp在每次搜索前重新加载索引?
解决ReadApp实时读取Lucene索引修改的配置方案
可以通过调整Hibernate Search的配置,让ReadApp每次读取前重新加载索引,从而获取WriteApp的最新修改,具体操作如下:
全局配置修改
修改ReadApp的Spring配置,添加索引刷新相关参数:
spring: jpa: properties: hibernate: search: indexing: plan.synchronization.strategy: sync listeners.enabled: true backend: directory.locking.strategy: none # 新增配置,强制每次读取前刷新索引 reader.strategy: read-only refresh: mode: always
配置说明
reader.strategy: read-only:适配ReadApp仅读取的场景,避免不必要的锁操作,贴合只读场景的特性。refresh.mode: always:强制Hibernate Search在每次执行查询前都刷新索引视图,确保能获取到WriteApp对索引的最新修改。
细粒度手动刷新(可选)
如果需要针对特定查询做刷新,而非全局强制刷新,可以在代码中通过SearchSession手动触发索引刷新:
// 获取SearchSession SearchSession searchSession = Search.session(entityManager); // 刷新目标实体的索引视图 searchSession.refresh(YourEntity.class); // 执行查询 List<YourEntity> results = searchSession.search(YourEntity.class) .where(f -> f.matchAll()) .fetchHits(20);
内容的提问来源于stack exchange,提问作者f1v3
相关产品推荐
相关产品推荐

