Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging Lucene.Net search results asp.net

Tags:

c#

lucene.net

i want to do pagination for lucene.net search result. when i fetch data from index then i need to fetch only 10 records in each page. so i search for lucene.net paging trick and i got a answer which is not clear to me. here it is...please have look.

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);

}

TopDocs topDocs = indexSearcher.Search(query, null, 150);
for(int i=100, i<min(topDocs.totalHits,150); i++) {
Document doc = indexSearcher.doc(topDocs.scoreDocs[i]);

// Do something with the doc
}

i just need to know is there any better technique for it. please discuss. thanks

From here my Update start

different way i was using to search index. after getting your code i tried to incoporate in my code but getting error. please have look at my code and convert it in such way as a result i can use your paging logic.

here is my code

            int PageIndex=0;
            int PageSize=10;
            searcher = new IndexSearcher(_directory, false);
            Query qry = MultiFieldQueryParser.Parse(Version.LUCENE_29, multiWordPhrase, fieldList, occurs.ToArray(), new StandardAnalyzer(Version.LUCENE_29));
            TopDocs topDocs = searcher.Search(qry, null, ((PageIndex + 1) * PageSize), Sort.RELEVANCE);

            int resultsCount = topDocs.TotalHits;
            lblMatchFound.Text = "Match Found " + resultsCount.ToString();

            List<SearchResult> list = new List<SearchResult>();
            SearchResult oSr = null;


            if (topDocs != null)
            {
                ScoreDoc[] scoreDocs = topDocs.ScoreDocs;
                foreach (ScoreDoc scoreDoc in scoreDocs)
                {
                    Document doc = searcher.Doc(scoreDoc.doc);
                    oSr = new SearchResult();
                    oSr.ID = doc.Get("ID");
                    oSr.Title = doc.Get("Title");
                    oSr.Description = doc.Get("Description");
                    //oSr.WordCount = AllExtension.WordCount(oSr.Description, WordExist(oSr.Title, multiWordPhrase));
                    string preview =
                    oSr.Description = AllExtension.HighlightKeywords(oSr.Description, multiWordPhrase);  //sr.Description;
                    oSr.Url = doc.Get("Url");
                    list.Add(oSr);
                }
            }

please have a look and restructure my code in such way i can do the paging. thanks

like image 530
Thomas Avatar asked Jul 12 '12 11:07

Thomas


1 Answers

First of all do not use Hits class since it is deprecated and slow.

For your paging case:

make a search for the first page like TopDocs td = s.Search(query, 10);

and for the second page TopDocs td = s.Search(query, 20); and display the results from 10 to 19

and so on...

PS: The costly part in Lucene is reading the results from the index, not the search itself. So above trick should perform very well.

-- EDIT (Untested) --

int page = 2; //starting from 0

TopDocs td = searcher.Search(query, (page+1)*10);
for (int i = page * 10; i < (page + 1) * 10 && i < td.scoreDocs.Length; i++)
{
    Document doc = indexReader.Document(td.scoreDocs[i].doc);
}
like image 182
L.B Avatar answered Nov 10 '22 00:11

L.B