0

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
pedjjj
  • 958
  • 3
  • 18
  • 40

1 Answers1

2

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
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • Oh okay, that is much simplier than I tought.. Thanks a lot! :-) – pedjjj Nov 27 '14 at 16:03
  • I updated the question a bit, because I forgot to mention that my query also contains other MUST clauses.. – pedjjj Nov 28 '14 at 08:51
  • 1
    thanks for your answer. Just wanted to mention that I compared the output of what you suggested (A OR B) AND C and "my" version +(A OR B) +C and it is the same. So both variants do work (I just had some other error, which irritated me). – pedjjj Nov 28 '14 at 14:30