Book Image

Lucene 4 Cookbook

By : Edwood Ng, Vineeth Mohan
Book Image

Lucene 4 Cookbook

By: Edwood Ng, Vineeth Mohan

Overview of this book

Table of Contents (16 chapters)
Lucene 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Pagination


Traditionally, when you display results from a query (which can be expensive at times), you may cache the results in memory and probably within a session only, so that you can feed the results back to the user in piecemeal by iterating through the result. This is one of the optimization techniques to minimize the number of queries to the search engine. In Lucene, the preferable way of handling pagination is to perform separate searches per page. Lucene's developers are confident enough that Lucene will perform well enough and any potential performance issues will outweigh the complexity of implementing your own pagination in memory.

One way to implement pagination is to use IndexSearcher's searchAfter method. It's similar to search with the addition of a ScoreDoc parameter in which the top results would return after the ScoreDoc. We can pass the last ScoreDoc of the previous page to searchAfter, so we can retrieve the following page.

How to do it...

Let's look at an example:

StandardAnalyzer...