[英]Paging Lucene.Net search results asp.net
我想為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);
}
}
請看一下我的代碼重構我可以做分頁。 謝謝
首先不要使用Hits
類,因為它已被棄用且速度很慢。
對於你的傳呼案例:
搜索第一頁,如TopDocs td = s.Search(query, 10);
對於第二頁TopDocs td = s.Search(query, 20);
並顯示10到19的結果
等等...
PS:Lucene中昂貴的部分是從索引中讀取結果,而不是搜索本身。 所以上面的技巧應該表現得非常好。
- 編輯(未經測試) -
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.