Difference between BooleanClause.Occur.Must and BooleanClause.Occur.SHOULD in lucene

Jagadesh picture Jagadesh · Apr 12, 2012 · Viewed 15.2k times · Source

Can anyone explain the difference between the BooleanClause.Occur.Must and BooleanClause.Occur.SHOULD in lucene in BooleanQuery with an example?

Answer

jpountz picture jpountz · Apr 12, 2012

BooleanClause.Occur.SHOULD means that the clause is optional, whereas BooleanClause.Occur.Must means that the clause is compulsory.

However, if a boolean query only has optional clauses, at least one clause must match for a document to appear in the results.

For better control over what documents match a BooleanQuery, there is also a minimumShouldMatch parameter which lets you tell Lucene that at least minimumShouldMatch BooleanClause.Occur.SHOULD clauses must match for a document to appear in the results.