When consideration of all the built-in features is exhausted and you still need more flexibility, it may be time to explore how to build a custom scoring mechanism to customize the search results ranking. Lucene has a CustomScoreQuery
class that allows you to do just that. We can provide our own score provider by extending from this class, along with CustomScoreProvider
. By extending CustomScoreProvider
, we can override score calculation with our own implementation.
Let's take a look at how it's done. We will build a CustomScoreQuery
that favors documents with terms that are anagrams of the querying Terms. For each anagram found, we increase the score by 1. The search results ranking will be augmented so that the documents with anagrammed Terms are ranked higher.
Here is the AnagramQuery
class:
public class AnagramQuery extends CustomScoreQuery { private final String field; private final Set<String> terms = new HashSet<String>(); public AnagramQuery...