aneuryzm
aneuryzm

Reputation: 64854

Lucene: Building a query of single terms

I'm new to Lucene and I would like to know what's the difference (if there is 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);

Upvotes: 3

Views: 2173

Answers (1)

dbyrne
dbyrne

Reputation: 61081

  • PhraseQuery requires that all the terms exist in the field being searched.
  • Your BooleanQuery does not require that all the terms exist.

This leads to the question of what is the difference between your PhraseQuery and:

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 the PhraseQuery would require the terms be in the correct order, as opposed to the BooleanQuery which would not have any particular order requirement.

Upvotes: 3

Related Questions