tags:

views:

55

answers:

1
+1  Q: 

Lucene.NET "OR"

How do I do an "OR" in Lucene.NET. Basically what I have is an array of IDs and I want to return any records where a particular field contains any of the values. I previously was doing this with just one value, but now I want to convert the following code so that MetaDataID is an array of possible values instead of one single value.

if (MetaDataID.Length > 0)
    completeQuery.Add(new QueryParser("MetaData", new StandardAnalyzer()).Parse(MetaDataID), BooleanClause.Occur.MUST);
+1  A: 

You need to use BooleanClause.Occur.SHOULD instead of BooleanClause.Occur.MUST

e.g.:

BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("id", "<id 1>"));
Query query2 = new TermQuery(new Term("id", "<id 2>"));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
Sam Doshi
this doesn't appear to return the set A|B. When I try this I'm getting all records that match the one other MUST clause. That is to say it appears as though the SHOULDs are being ignored.
Kyle
Not quite sure I understand you. But I think what you need to to is nest your BooleanQueries.
Sam Doshi