Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I boost queries in Lucene 7?

Tags:

java

lucene

I want to boost a query in Lucene 7. In the previous versions (< 6) I was just using the setBoost(float boost) method. i.e.

TermQuery termQuery = new TermQuery(new Term("field", "value"));
termQuery.setBoost(2);

In Lucene 7 there is only a method that contains the boost as a parameter:

public Weight createWeight(IndexSearcher searcher,
                       boolean needsScores,
                       float boost)

which isn't the method responsible for the boost! Do you know how to apply the boosting to the queries?

like image 205
pokeRex110 Avatar asked Mar 08 '23 14:03

pokeRex110


1 Answers

All queries are now immutable, which also extends to boosts, per LUCENE-6590. As such, to apply boosts you would use a BoostQuery to wrap the query. Like this:

Query termQuery = new TermQuery(new Term("field", "value"));
Query boostedTermQuery = new BoostQuery(termQuery, 2);
like image 64
femtoRgon Avatar answered Mar 10 '23 05:03

femtoRgon