Lucene: building a query for individual terms

I am new to Lucene and I would like to know what is the difference (if any) between

PhraseQuery.add(Term1)
PhraseQuery.add(Term2)
PhraseQuery.add(Term3)

and

term1 = new TermQuery(new Term(...));
booleanQuery.add(term1, BooleanClause.Occur.SHOULD);    

term2 = new TermQuery(new Term(...));
booleanQuery.add(term2, BooleanClause.Occur.SHOULD);

term3 = new TermQuery(new Term(...));
booleanQuery.add(term3, BooleanClause.Occur.SHOULD);
+3
source share
1 answer
  • PhraseQuery requires that all terms exist in the field under study.
  • Yours BooleanQuerydoes not require all terms.

This leads to the question of what is the difference between yours PhraseQueryand:

term1 = new TermQuery(new Term(...));
booleanQuery.add(term1, BooleanClause.Occur.MUST);    

term2 = new TermQuery(new Term(...));
booleanQuery.add(term2, BooleanClause.Occur.MUST);

term3 = new TermQuery(new Term(...));
booleanQuery.add(term3, BooleanClause.Occur.MUST);

The difference here is that it PhraseQuerywill require the members to be in the correct order, as opposed to BooleanQuerythat which would not have any particular order requirement.

+3
source

All Articles