Chapter 6. Extending search
This chapter covers
- Creating a custom sort
- Using a Collector
- Customizing QueryParser
- Using positional payloads
Just when you thought we were done with searching, here we are again with even more on the topic! Chapter 3 discussed the basics of Lucene’s built-in capabilities, and chapter 5 delved well beyond the basics into Lucene’s more advanced searching features. In those two chapters, we explored only the built-in features. Lucene also has several powerful extension points, which we’ll cover here.
Custom sorting lets you implement arbitrary sorting criteria when the built-in sort by relevance or field isn’t appropriate. We’ll show an example of sorting by geographic proximity to a user’s current location. Custom collection lets you arbitrarily process each matching document yourself, in case you don’t want the top documents according to a sort criterion. We’ll also include examples of two custom collectors. QueryParser has many extension points to customize how each type of query is created, and we provide examples, including how to prevent certain query types and handling numeric and date fields. Custom filters let you arbitrarily restrict the allowed documents for matching. Finally, you can use payloads to separately boost specific occurrences of a given term within the same document. Armed with an understanding of these powerful extension points, you’ll be able to customize Lucene’s behavior in nearly arbitrary ways.