如何在lucene中实现分页? [英] how to achieve pagination in lucene?

查看:139
本文介绍了如何在lucene中实现分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何在Lucene中实现分页,因为它本身并不支持分页.我基本上需要搜索前10个条目"(基于某个参数),然后搜索下10个条目",依此类推.同时,我不希望Lucene浪费记忆. 任何建议,将不胜感激. 预先感谢.

Wondering how to achieve pagination in Lucene, as it does not inherently support pagination. I basically need to search for 'top 10 entries' (based on some parameter) then 'next 10 entries' and so on. And at the same time I don't want Lucene to hog memory. Any piece of advice would be appreciated. Thanks in advance.

推荐答案

您将需要应用自己的分页机制,类似于下面的内容.

You will need to apply your own paging mechanism, something similar to that below.

 IList<Document> luceneDocuments = new List<Document>();

 IndexReader indexReader = new IndexReader(directory);
 Searcher searcher = new IndexSearcher(indexReader);

 TopDocs results = searcher.Search("Your Query", null, skipRecords + takeRecords);
 ScoreDoc[] scoreDocs = results.scoreDocs;

 for (int i = skipRecords; i < results.totalHits; i++)
 {
      if (i > (skipRecords + takeRecords) - 1)
      {
           break;
      }

      luceneDocuments.Add(searcher.Doc(scoreDocs[i].doc));
 }

您会发现,对scoreDocs数组进行迭代将是轻量级的,因为直到调用searcher.Doc方法才真正使用索引中包含的数据.

You will find that iterating the scoreDocs array will be lightweight as the data contained within the index is not really used until the searcher.Doc method is called.

请注意,此示例是针对稍有修改的Lucene.NET 2.3.2版本编写的,但是基本主体应可针对任何最新版本的Lucene使用.

Please note that this example was written against a slightly modified version of Lucene.NET 2.3.2, but the basic principal should work against any recent version of Lucene.

这篇关于如何在lucene中实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆