我如何在Lucene.NET中进行“或”操作。基本上,我拥有的是ID数组,我想返回任何记录,其中特定字段包含任何值。我以前只用一个值来执行此操作,但是现在我想转换以下代码,以便MetaDataID是可能值的数组,而不是一个值。
if (MetaDataID.Length > 0)
completeQuery.Add(new QueryParser("MetaData", new StandardAnalyzer()).Parse(MetaDataID), BooleanClause.Occur.MUST);
最佳答案
将Lucene查询合并到希望包含任何多个可能值中的任何一个的索引记录与还必须满足的其他条件结合使用时,请创建多个 bool 查询对象。
对于第一组“OR”条件:
BooleanQuery booleanQueryInner = new BooleanQuery();
Query query1 = new TermQuery(new Term("id", "<id 1>"));
Query query2 = new TermQuery(new Term("id", "<id 2>"));
Query query3 = new TermQuery(new Term("id", "<id 3>"));
Query query4 = new TermQuery(new Term("id", "<id 4>"));
booleanQueryInner.add(query1, BooleanClause.Occur.SHOULD);
booleanQueryInner.add(query2, BooleanClause.Occur.SHOULD);
booleanQueryInner.add(query3, BooleanClause.Occur.SHOULD);
booleanQueryInner.add(query4, BooleanClause.Occur.SHOULD);
现在结合查询中的其他条件
BooleanQuery booleanQueryOuter = new BooleanQuery();
booleanQueryOuter.add(booleanQueryInner, BooleanClause.Occur.MUST);
booleanQueryOuter.add(boolenaQueryOtherConditions, BooleanClause.Occur.MUST);
现在,仅当索引记录满足内部“OR”组中的条件之一并且还满足“其他条件”查询中的条件时,才返回索引记录。
关于Lucene.NET "OR",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2056908/