使用标准查询解析器(您可以查看相关文档),您可以使用类似于 DB 查询的语法,例如:
(country:brazil OR country:france OR country:china) AND (other search criteria)
或者,为了简化一点:
country:(brazil OR france OR china) AND (other search criteria)
或者,Lucene 还支持使用 +/- 而不是 AND/OR 语法编写的查询。我发现该语法对于 Lucene 查询更具表现力。这种形式的等价物是:
+country:(brazil france china) +(other search criteria)
如果手动构建查询,您确实可以嵌套BooleanQueries以创建类似的结构,使用正确的BooleanClauses来建立您指定的 And/Or 逻辑:
Query countryQuery = new BooleanQuery();
countryQuery.add(new TermQuery(new Term("country","brazil")),BooleanClause.Occur.SHOULD);
countryQuery.add(new TermQuery(new Term("country","france")),BooleanClause.Occur.SHOULD);
countryQuery.add(new TermQuery(new Term("country","china")),BooleanClause.Occur.SHOULD);
Query otherStuffQuery = //Set up the other query here,
//or get it from a query parser, or something
Query rootQuery = new BooleanQuery();
rootQuery.add(countryQuery, BooleanClause.Occur.MUST);
rootQuery.add(otherStuffQuery, BooleanClause.Occur.MUST);