我使用以下代码进行搜索,使用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();
答案 0 :(得分:3)
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();
答案 1 :(得分:2)
OR 逻辑是Lucene的默认逻辑。您可以使用此处所述的布尔DSL查询 - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-query-querydsl,但这可能无法解决您的问题,因为您似乎在一个字符串中同时包含两个查询字词。根据您的用例(例如,如果用户提供搜索字符串),从Lucene查询解析器获取Lucene查询可能更好。