[英]And operator in Hibernate (lucene) search
我正在使用以下代码使用hibernate-search进行搜索。 但这会标记搜索查询并进行“ 或”搜索 ,而我想进行“ 与”搜索 。 我怎么做?
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();
对于Lucene,“ 或”逻辑是默认设置。 您可以按照以下说明使用布尔DSL查询-http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-query-querydsl ,但这可能无法解决您的问题但是,因为您似乎将两个查询词都放在一个字符串中。 根据您的用例(例如,搜索字符串由用户提供),从Lucene查询解析器获取Lucene查询可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.