Lucene 4分页 [英] Lucene 4 Pagination
问题描述
我正在使用Lucene 4.2并且正在实施结果分页。
I am using Lucene 4.2 and am implementing result pagination.
IndexSearcher.searchAfter
提供了一种有效的方式实现下一页功能,但实现上一页甚至转到页面功能的最佳方法是什么?例如,没有 IndexSearcher.searchBefore
。
IndexSearcher.searchAfter
provides an efficient way of implementing "next page" functionality but what is the best way to go about implementing "previous page" or even "go to page" functionality? There is no IndexSearcher.searchBefore
for example.
我正在考虑确定给定页面大小的总页数并保持一个 ScoreDoc []
数组来跟踪每个页面的after ScoreDoc
(该数组将被填充为结果被分页)。这将允许我使用最近的 ScoreDoc
在 IndexSearcher.searchAfter
中使用(或在最坏的情况下为null) )。
I was considering determining the total number of pages given the page size and keeping a ScoreDoc[]
array to track the "after" ScoreDoc
for each page (the array would be populated as results are paged in). This would allow me to use the "closest" ScoreDoc
for use in IndexSearcher.searchAfter
(or null in the worst case).
这有意义吗?有没有更好的方法?
Does this make sense? Is there a better approach?
推荐答案
我一直在使用Lucene 4.8并且一直在开发包含分页的REST接口。
我的解决方案是使用TopScoreDocCollector并调用topDocs(int startIndex,int numberOfhits)方法。起始索引的计算方法是将基于零的页码乘以命中数。
I've been using Lucene 4.8 and have been working on a REST interface which includes pagination. My solution has been to use a TopScoreDocCollector and call the topDocs(int startIndex, int numberOfhits) method. The start index is calculated by multiplying the zero based page number by the number of hits.
...
DirectoryReader reader = DirectoryReader.open(MMapDirectory.open( java.io.File(indexFile) );
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(MAX_RESULTS, true); // MAX_RESULTS is just an int limiting the total number of hits
int startIndex = (page -1) * hitsPerPage; // our page is 1 based - so we need to convert to zero based
Query query = new QueryParser(Version.LUCENE_48, "All", analyzer).parse(searchQuery);
searcher.search(query, collector);
TopDocs hits = collector.topDocs(startIndex, hitsPerPage);
...
所以我的REST接口接受页码和每页点击次数作为参数。
所以前进或后退就像提交具有适当页面值的新请求一样简单
So my REST interface accepts the page number and number of hits per page as parameters. So going forward or back is as simple as submitting a new request with the appropriate value for the page
这篇关于Lucene 4分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!