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.
Search Lucene Docs. To search lucene documents, you need to create org.apache.lucene.search.Query instance using org.apache.lucene.queryparser.classic.QueryParser class. IndexSearcher.seach(Query) returns org.apache.lucene.search.TopDocs which represents the query result.
What is Lucene? Lucene is a program library published by the Apache Software Foundation. It is open source and free for everyone to use and modify. Originally, Lucene was written completely in Java, but now there are also ports to other programming languages.
You can use Lucene instead: a free open source project from Apache. Numerous companies have integrated Apache Lucene – either online or offline. Until a few years ago, Wikipedia implemented Lucene as a search function, but now uses Solr, which is based on Lucene. Twitter’s search runs completely on Lucene.
Apache Solr and Elasticsearch are powerful extensions that give the search function even more possibilities. Lucene is a full-featured text search. This means, quite simply: a program searches a series of text documents for one or more terms that the user has specified.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With