Hibernate搜索FullTextFilter缓存无法正常工作

时间:2018-04-18 14:07:07

标签: hibernate lucene hibernate-search lucene-filters

我有以下实体声明:

@FullTextFilterDefs({
  @FullTextFilterDef(name = "withStock", impl = WithStockFilterFactory.class, cache = FilterCacheModeType.INSTANCE_ONLY),
  @FullTextFilterDef(name = "onlyActive", impl = OnlyActiveFilterFactory.class, cache = FilterCacheModeType.INSTANCE_ONLY),
})
public class Article {

    @Column
    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.NO)
    protected Boolean activo;
}

这是我的过滤器:

public class OnlyActiveFilterFactory {

  private Boolean onlyActive;

  public void setOnlyActive(Boolean onlyActive) {
    this.onlyActive = onlyActive;
  }

  @Factory
  public Query getFilter() {
    List<Term> terms = new ArrayList<>();
    terms.add(new Term("active", "true"));

    if (!onlyActive) {
      terms.add(new Term("active", "false"));
    }

    return new QueryWrapperFilter(new TermsQuery(terms));
  }
}

但是当我执行搜索时,一切都没问题,直到我为实例更改active的值,因为尽管参数onlyActive的值设置为过滤器;对于其他每个实例工作正常。

直到我重建整个索引才会发生这种情况。我试图在更改它的active值之后重新索引实体,就像这样,但没有运气:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.index(article);

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

没关系, 存储库中更改active属性值的方法使用@Query进行了注释,以便对数据库的操作不会强制重新索引和缓存更新。