I have implemented pagination with Lucene using the searchAfter
method provided by IndexSearcher
. In every call, I pass the last ScoreDoc
returned in the previous page.
The problem is that sometimes, the index gets updated between page and page and occasionally I am getting this exception:
java.lang.IllegalArgumentException: after.doc exceeds the number of
documents in the reader: after.doc=337 limit=337
at
org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:434)
I understand that Lucene changes the docs ids every now and then (segment merges, etc.) and I guess that is why that exception is happening as searchAfter
relies on those docs ids.
How could I improve this pagination mechanism to avoid this exception? Is there any better way to implement pagination with Lucene?