Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging using Lucene.net

I'm working on a .Net application which uses Asp.net 3.5 and Lucene.Net I am showing search results given by Lucene.Net in an asp.net datagrid. I need to implement Paging (10 records on each page) for this aspx page.

How do I get this done using Lucene.Net?

like image 415
user40907 Avatar asked Nov 26 '08 04:11

user40907


1 Answers

Here is a way to build a simple list matching a specific page with Lucene.Net. This is not ASP.Net specific.

int first = 0, last = 9; // TODO: Set first and last to correct values according to page number and size
Searcher searcher = new IndexSearcher(YourIndexFolder);
Query query = BuildQuery(); // TODO: Implement BuildQuery
Hits hits = searcher.Search(query);
List<Document> results = new List<Document>();
for (int i = first; i <= last && i < hits.Length(); i++)
    results.Add(hits.Doc(i));

// results now contains a page of documents matching the query

Basically the Hits collection is very lightweight. The cost of getting this list is minimal. You just instantiate the needed Documents by calling hits.Doc(i) to build your page.

like image 66
David Thibault Avatar answered Oct 18 '22 01:10

David Thibault