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
(数组将在结果被分页时填充).这将允许我在 IndexSearcher.searchAfter
中使用最接近的"ScoreDoc
(或在最坏的情况下为 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屋!