Hibernate Search(lucene)中是否有可能组合两个不同的查询。例如,当我要搜索2个字段,这些字段应具有一个对应的匹配值时:

firstname - John
lastname - Doe

qBuilder.keyword().onField("firstname").matching("John").createQuery());
qBuilder.keyword().onField("lastname").matching("Doe").createQuery());


这是一种查询方法吗?

最佳答案

注意:这可行,但是要获得更多类似Hibernate的方法,请参阅我的其他答案

createQuery()返回标准的Lucene Query。因此,在Lucene中合并两个查询的典型方法是使用BooleanQuery

Query query1 = qBuilder.keyword().onField("firstname").matching("John").createQuery();
Query query2 = qBuilder.keyword().onField("lastname").matching("Doe").createQuery();
BooleanQuery bq = new BooleanQuery();
//Assuming you want to require a match on both first and last names.
//If a match on either is enough, use BooleanClause.Occur.SHOULD
bq.add(new BooleanClause(query1, BooleanClause.Occur.MUST));
bq.add(new BooleanClause(query2, BooleanClause.Occur.MUST));

10-06 16:08