Search code examples
javaluceneinformation-retrieval

Lucene - Is it possible to change a query's score without changing the query itself?


I'm trying to implement a relevance feedback feature on my program using one of the available algorithms. This requires changing the original query score by adding it with the total of all relevant document's scores. The query with improved score should give new results when used. The problem is, so far I haven't found any way to change the query score on Lucene. Did I miss something on the documentation? Does Lucene provide a way to change an existing query's score?


Solution

  • To modify a query's score you wrap that query inside another query. There are several options for doing that in Lucene. Two very relevant ones:

    • FunctionScoreQuery: Provides static methods to wrap a query multiplying the original score by either the result of another query or a DoubleValuesSource that produces a boost per each document in the original set.
    • The expression module: This allows you to make arbitrary calculations to generate a score. It can be combined with FunctionScoreQuery above. Check the docs for more info.