How do I boost queries in Lucene 7?

Viewed 2139

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?

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);
Related