Java 结合hibernate全文搜索和JPA查询

Java 结合hibernate全文搜索和JPA查询,java,hibernate,jpa-2.1,Java,Hibernate,Jpa 2.1,我最近熟悉了Hibernate搜索API。如何对实体进行全文搜索,并按一个特定字段过滤结果?例如: final FullTextEntityManager ftem = Search.getFullTextEntityManager(em); final QueryBuilder qb = ftem.getSearchFactory() .buildQueryBuilder().forEntity(Item.class).get(); final org.apache.luc

我最近熟悉了Hibernate搜索API。如何对实体进行全文搜索,并按一个特定字段过滤结果?例如:

final FullTextEntityManager ftem = Search.getFullTextEntityManager(em);

final QueryBuilder qb = ftem.getSearchFactory()
        .buildQueryBuilder().forEntity(Item.class).get();

final org.apache.lucene.search.Query query = qb
        .keyword()
        .onFields(Item_.CONTENT)
        .matching(match)
        .createQuery();

/* also here I want to filter the results for all the Items in which category equals to 2 */

final FullTextQuery persistenceQuery = ftem.createFullTextQuery(query, Item.class);

final List<Item> result = persistenceQuery.getResultList();
final FullTextEntityManager ftem=Search.getFullTextEntityManager(em);
final QueryBuilder qb=ftem.getSearchFactory()
.buildQueryBuilder().forEntity(Item.class).get();
final org.apache.lucene.search.Query Query=qb
.keyword()
.onFields(项目内容)
.匹配(匹配)
.createQuery();
/*在这里,我还想筛选类别等于2的所有项目的结果*/
final FullTextQuery persistenceQuery=ftem.createFullTextQuery(查询,Item.class);
最终列表结果=persistenceQuery.getResultList();

对于这种情况,您可以使用Hibernate搜索过滤器。您还需要为筛选所需的分词字段编制索引,尽管查询本身不需要它


我自己用,效果很好

我也有同样的问题!不幸的是,我还没有发现任何有用的东西。如果你找到了解决方案,也请分享答案。谢谢你的回答。但我不知道索引例如userId字段并对其应用过滤器是否正确!!我可能有20000000条记录,其中1000000条有相同的用户ID。在用户ID上创建过滤器是否足够快?我不确定这是否真的是个问题。如果你用一些简单的分析器来分析这个字段。如果您想在SQL DB中过滤该字段,您可能还需要为该字段使用一些索引。