I am using Lucene to show search results in a web application.I am also custom paging for showing the same. Search results could vary from 5000 to 10000 or more. Can someone please tell me the best strategy for paging and caching the search results?
Asked
Active
Viewed 1.1k times
19
-
2This user seems to be re-asking the same or similar questions repeatedly: http://stackoverflow.com/users/41625/meyahoocomaodzxyowykprbavs5sf701zowgzpc3svjv8 Look at the posting record before taking the time to answer... – James Brady Dec 25 '08 at 23:41
1 Answers
31
I would recommend you don't cache the results, at least not at the application level. Running Lucene on a box with lots of memory that the operating system can use for its file cache will help though.
Just repeat the search with a different offset for each page. Caching introduces statefulness that, in the end, undermines performance. We have hundreds of concurrent users searching an index of over 40 million documents. Searches complete in much less than one second without using explicit caching.
Using the Hits
object returned from search, you can access the documents for a page like this:
Hits hits = searcher.search(query);
int offset = page * recordsPerPage;
int count = Math.min(hits.length() - offset, recordsPerPage);
for (int i = 0; i < count; ++i) {
Document doc = hits.doc(offset + i);
...
}

erickson
- 265,237
- 58
- 395
- 493
-
Hello erickson.. can you please tell me from which version onwards the lucene supports the search method taking only 1 argument of query..and also is sorting possible with this solution.??? – Asha Koshti Mar 20 '14 at 08:54
-
@AshaKoshti Forever? It was there in 1.4.3, released in 2004. I can't find a version older than that in the archives. Yes, there's a `search(Query, Sort)` overload on `Searcher` too. – erickson Mar 20 '14 at 16:14
-
@erickson ..it has this three versions of search in 1.4.3 .. 1) TopDocs search(Query query, Filter filter, int nDocs) 2) TopFieldDocs search(Query query, Filter filter, int nDocs, Sort sort) 3) void search(Query query, Filter filter, HitCollector results).. I am not able to find the one which you were talking about i.e. searcher.search(query); ..??.. – Asha Koshti Mar 21 '14 at 05:10
-
@AshaKoshti You are looking at the `Searchable` interface. I am referring to methods declared in the `org.apache.lucene.search.Searcher` class. – erickson Mar 21 '14 at 15:45
-
1@erickson .. It was available in 1.4.3 version .but after that it is not available even in stable version 3.5 or latest one 4.0 .. – Asha Koshti Mar 24 '14 at 06:45