tags:

views:

612

answers:

2

Wondering how to achieve pagination in Lucene, as it does not inherently support pagination. I basically need to search for 'top 10 entries' (based on some parameter) then 'next 10 entries' and so on. And at the same time I don't want Lucene to hog memory. Any piece of advice would be appreciated. Thanks in advance.

+5  A: 

You will need to apply your own paging mechanism, something similar to that below.

 IList<Document> luceneDocuments = new List<Document>();

 IndexReader indexReader = new IndexReader(directory);
 Searcher searcher = new IndexSearcher(indexReader);

 TopDocs results = searcher.Search("Your Query", null, skipRecords + takeRecords);
 ScoreDoc[] scoreDocs = results.scoreDocs;

 for (int i = skipRecords; i < results.totalHits; i++)
 {
      if (i > (skipRecords + takeRecords) - 1)
      {
           break;
      }

      luceneDocuments.Add(searcher.Doc(scoreDocs[i].doc));
 }

You will find that iterating the scoreDocs array will be lightweight as the data contained within the index is not really used until the searcher.Doc method is called.

Please note that this example was written against a slightly modified version of Lucene.NET 2.3.2, but the basic principal should work against any recent version of Lucene.

Kane
I agree, results in Lucene are not as heavy as the results when querying a database so you can easily implement custom pagination methods without having to deal with performance issues
victor hugo
A: 

Q1.-How Lucene search works? Q2.-If I use text while adding documents then after adding documents if I delete the database then can I search the records from the index & view the searched results?