使用标准查询解析器(你可以看看relevant documentation),你可以使用类似于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 来建立您指定的与/或逻辑:
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);