[英]Paging Lucene.Net search results asp.net
i want to do pagination for lucene.net search result. 我想为lucene.net搜索结果做分页。 when i fetch data from index then i need to fetch only 10 records in each page.
当我从索引中获取数据时,我需要在每个页面中只获取10条记录。 so i search for lucene.net paging trick and i got a answer which is not clear to me.
所以我搜索lucene.net分页技巧,我得到了一个我不清楚的答案。 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
谢谢
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.
请查看我的代码并以这样的方式转换它,结果我可以使用您的分页逻辑。
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
谢谢
First of all do not use Hits
class since it is deprecated and slow. 首先不要使用
Hits
类,因为它已被弃用且速度很慢。
For your paging case: 对于你的传呼案例:
make a search for the first page like TopDocs td = s.Search(query, 10);
搜索第一页,如
TopDocs td = s.Search(query, 10);
and for the second page TopDocs td = s.Search(query, 20);
对于第二页
TopDocs td = s.Search(query, 20);
and display the results from 10 to 19 并显示10到19的结果
and so on... 等等...
PS: The costly part in Lucene is reading the results from the index, not the search itself. PS:Lucene中昂贵的部分是从索引中读取结果,而不是搜索本身。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.