Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lucene's API, can I boost terms?

With Lucene's query parser, it's possible to boost terms (causing them to weight higher in the search results) by appending "^n", e.g. "apple^5 pie" would assign five times more importance to the term "apple". Is it possible to do this when constructing a query using the API? Note that I'm not wanting to boost fields or documents, but individual terms within a field.

like image 590
Frungi Avatar asked Mar 27 '26 23:03

Frungi


1 Answers

You simply need to use the setBoost(float) method of the Query class.

For example

TermQuery tq1 = new TermQuery(new Term("text", "term1"));
tq1.setBoost(5f);
TermQuery tq2 = new TermQuery(new Term("text", "term2"));
tq2.setBoost(0.8f);
BooleanQuery query = new BooleanQuery();
query.add(tq1, Occur.SHOULD);
query.add(tq2, Occur.SHOULD);

This is equivalent to parsing the query text:term1^5 text:term2^0.8.

like image 93
jpountz Avatar answered Mar 30 '26 08:03

jpountz



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!