我想为 lucene.net 搜索结果进行分页。当我从索引中获取数据时,我只需要在每页中获取 10 条记录。所以我搜索 lucene.net 寻呼技巧,我得到了一个我不清楚的答案。在这里……请看。
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
}
我只需要知道有没有更好的技术。请讨论。谢谢
从这里我的更新开始
我用来搜索索引的不同方式。获得您的代码后,我尝试将其纳入我的代码中,但出现错误。请查看我的代码并以这种方式进行转换,以便我可以使用您的分页逻辑。
这是我的代码
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);
}
}
请查看并以我可以进行分页的方式重组我的代码。谢谢