CustomScoreQuery

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.

How to do it…

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 ...

Get Lucene 4 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.