我正在使用以下代码使用 hibernate-search 进行搜索。但这会将搜索查询标记化并进行OR 搜索,而我想进行AND 搜索。我该怎么做?
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
String searchQuery = "test query";
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Customer.class).get();
TermMatchingContext onFields = qb.keyword().onFields("customer.name","customer.shortDescription","customer.longDescription");
org.apache.lucene.search.Query query = onFields.matching(searchQuery).createQuery();
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Customer.class);
List<Customization> result = persistenceQuery.getResultList();
最佳答案
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Customer.class).get();
TermMatchingContext onFields = qb.keyword().onFields("customer.shortDescription", "customer.longDescription");
BooleanJunction<BooleanJunction> bool = qb.bool();
org.apache.lucene.search.Query query = null;
String[] searchTerms = searchQuery.split("\\s+");
for (int j = 0; j < searchTerms.length; j++) {
String currentTerm = searchTerms[j];
bool.must(onFields.matching(currentTerm).createQuery());
}
query = bool.createQuery();
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Customer.class);
resultList = persistenceQuery.getResultList();
关于java - Hibernate (lucene) 搜索中的 And 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863122/