Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene: Building a query of single terms

Tags:

lucene

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);
like image 610
aneuryzm Avatar asked Nov 28 '25 21:11

aneuryzm


1 Answers

  • 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.

like image 101
dbyrne Avatar answered Dec 01 '25 19:12

dbyrne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!