0
votes

How can QueryParser from Apache Lucene be used to have a query that either contains term A or term B (or both, but at least one of them). EDIT: Also, it contains other MUST clauses, e.g., it must contain C and it must contain either A or B.

Is this then correct?

+(A OR B) +C
1

1 Answers

2
votes

It is simply: A OR B (or just A B)

This will generate two BooleanClauses with Occur.SHOULD. In this case, at least one clause has to match for the whole BooleanQuery to match:

/** Use this operator for clauses that <i>should</i> appear in the 
 * matching documents. For a BooleanQuery with no <code>MUST</code> 
 * clauses one or more <code>SHOULD</code> clauses must match a document 
 * for the BooleanQuery to match.
 * @see BooleanQuery#setMinimumNumberShouldMatch
 */
SHOULD   { @Override public String toString() { return "";  } },

Answer to the updated question:

(A OR B) AND C should do what you want.

I'm not really sure about +(A OR B) +C since it looks like it should work but you stated that +(A OR B) doesn't work like you expect it to in your original question.

To make sure, you can take a look at what QueryParser generates. You'd need this kind of query structure:

  • BooleanQuery
    • Must: BooleanQuery
      • Should: TermQuery: A
      • Should: TermQuery: B
    • Must: TermQuery: C