You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 05:42:42