Lucene - "AND"套"OR"术语

Kev*_*vin 0 lucene lucene.net

假设我使用列表国家等标准进行搜索.用户可以选择要搜索的一组国家/地区,并将此组合与其他条件相结合.

在SQL中我会在我的where子句中执行此操作,即WHERE(country ='brazil'OR country ='france'OR country ='china)AND(其他搜索条件).

目前尚不清楚如何在Lucene这样做.Query.combine似乎有希望但如果我有多套"OR"条款可以解决,那么复杂性会很快增加.

Lucene在这方面有能力吗?或者我应该使用这些类型的标准点击我的常规数据库并过滤我的Lucene结果?

深入挖掘,看起来您可以嵌套布尔查询来完成此任务.如果这种技术有效,我会更新答案,如果它是高效的.

fem*_*gon 5

使用标准查询解析器(您可以查看相关文档),您可以使用类似于DB查询的语法,例如:

 (country:brazil OR country:france OR country:china) AND (other search criteria)
Run Code Online (Sandbox Code Playgroud)

或者,简化一下:

 country:(brazil OR france OR china) AND (other search criteria)
Run Code Online (Sandbox Code Playgroud)

或者,Lucene还支持使用+/-而不是AND/OR语法编写的查询.我发现Lucene查询的语法更具表现力.这种形式的等价物是:

 +country:(brazil france china) +(other search criteria)
Run Code Online (Sandbox Code Playgroud)

如果手动构造查询,您确实可以嵌套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);
Run Code Online (Sandbox Code Playgroud)