flaviumanica
flaviumanica

Reputation: 195

Apache Lucene boost document section

I am working on a project in Apache Lucene 7.2.1 and I want to change the scoring system for documents, so that the first part of a document (first 5 words) are twice more relevant than the rest of the document.

As an example:

doc1 = "one two three four five six"

doc2 = "six one two three four five"

query = "six"

The score for doc2 must be twice larger than the score for doc1.

Can you please help me achieve this? I know that in older versions of Lucene there was a setBoost method on Field, but in this version, there isn't one. Should the boost be set when a document is indexed, or when the query is made?

Thank you!

Upvotes: 0

Views: 506

Answers (1)

dom
dom

Reputation: 763

Boosting should be done while search-time. You're able to achieve this with a BoostQuery.

BoostQuery is Query class so you're able to combine this with other query types. An abstract example:

BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(new BoostQuery(query1, 2f), BooleanClause.Occur.MUST);
booleanQuery.add(new BoostQuery(query2, 1f), BooleanClause.Occur.MUST);

See more details for general scoring and boosting here: https://lucene.apache.org/core/7_0_0/core/org/apache/lucene/search/package-summary.html#package.description

Upvotes: 1

Related Questions