《Lucene in action》中推荐的Lucene分页方式
2006-12-23 17:38
288 查看
在《Lucene in action》 中专门讨论了如何对Hits进行分页,提供了两条路:一是在用户进行分页导航的过程中保持Hits和IndexSearcher的可用状态,也就是说不能在搜索之后关闭IndexSearcher,并且还要将Hits保存到Session中。而推荐的做法是为每一次分页导航都执行一次新的查询。因为Hits中保存的并不是真正的Document,因此可以通过HIts.doc(index)的方式取出在一定范围内的Document。在获得Hits后可以用类似下面的方法进行分页处理:
private List processHits(Hits hits,int startIndex,int endIndex)throws Exception{
if(endIndex>=hits.length())
endIndex=hits.length()-1;
List docs=new ArrayList();
for(int i=startIndex;i<=endIndex;i++){
Document doc=hits.doc(i);
Map docMap=new HashMap();
docMap.put("id",doc.getField("id").stringValue());
docMap.put("name",doc.getField("name").stringValue());
docMap.put("price",doc.getField("price").stringValue());
docs.add(docMap);
}
return docs;
}
这里可以按自己的需要重新封装Document和Field的数据。startIndex和endIndex标定了当前页面的范围。
private List processHits(Hits hits,int startIndex,int endIndex)throws Exception{
if(endIndex>=hits.length())
endIndex=hits.length()-1;
List docs=new ArrayList();
for(int i=startIndex;i<=endIndex;i++){
Document doc=hits.doc(i);
Map docMap=new HashMap();
docMap.put("id",doc.getField("id").stringValue());
docMap.put("name",doc.getField("name").stringValue());
docMap.put("price",doc.getField("price").stringValue());
docs.add(docMap);
}
return docs;
}
这里可以按自己的需要重新封装Document和Field的数据。startIndex和endIndex标定了当前页面的范围。
相关文章推荐
- 推荐《Lucene In Action》一书
- Unable to instantiate Action, xxxAction, defined for 'xxx' in namespace '/'xxxAction解决方式
- 《Lucene in Action》 MoreLikeThis 实例
- (原创)《Lucene in Action 2nd Edition》Chapter11.2.2中的工具类SearcherManager翻译为C#版本
- Lucene分页方式
- Mahout in Action Chapter 3 推荐数据的表示
- lucene&nbsp;in&nbsp;action&nbsp;第一章
- 蛙蛙推荐:sqlserver三种分页方式性能比较
- 《Lucene In Action》第四章.Analysis(分词)
- 《Lucene In Action》第三章.搜索
- 《Lucene in Action》 MoreLikeThis 实例
- 好书推荐-Spring in Action
- lucene&nbsp;in&nbsp;action第二章(1)(深…
- lucene结果集的分页处理(百度 分页 方式)终于调试通过了。
- SQL Server 分页编号的另一种方式【推荐】
- Lucene in action 第一章 初识Lucene
- 《Lucene in Action》(第二版) 第二章节的学习总结 ---- IndexWriter+Document+Field
- lucene&nbsp;in&nbsp;action第二章(2)(深…
- Lucene in action 笔记 term vector
- LuceneInAction小结一下