请问用Lucene searchafter分页,怎样排序?
@Test public void testsearch() throws Exception { LuceneResult luceneResult = new LuceneResult(1, 2); Sort sort = new Sort(); sort.setSort(new SortField(GoodsLuceneVo.STORE_PRICE,Type.DOUBLE)); for (int i = 1; i < 6; i++) { luceneResult.setCurrentPage(luceneResult.getCurrentPage() + 1); luceneResult = goodsLuceneUtil.goodsPage("鞋", goodsIndexPath, 0.0D, 0.0D, luceneResult, sort); List<GoodsLuceneVo> goods = luceneResult.getGoodsVo_list(); for (GoodsLuceneVo vo : goods) { System.out.println("id:" + vo.getVo_id()); System.out.println("标题:" + vo.getVo_title()); System.out.println("价格:" + vo.getVo_store_price()); System.out.println("添加时间:" + vo.getVo_add_time()); System.out.println("内容:" + vo.getVo_content()); } System.out.println("当前页为:" + luceneResult.getCurrentPage()+ "========================="); } }
没有sort排序的时候,可以分页,加上sort,就会出现下面的错误
但是ScoreDoc 怎么转化为FieldDoc ,Field.fields不知到怎么赋值,不理解什么意思,然后就是,用这种searchafter方法能排序吗,他内部是怎样实现的啊,求各位大神帮忙,谢谢
java.lang.IllegalArgumentException: after must be a FieldDoc; got doc=46 score=0.18231343 shardIndex=-1 at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:396)
/** * Lucene分页查询 * * @param directoryPath * @param query * @param page * @throws IOException */ public static void pageQuery(Query query, LuceneResult result, Sort sort) throws IOException { IndexSearcher searcher = createIndexSearcher(index_path); ScoreDoc after = getLastScoreDoc(result.getCurrentPage(), result.getPageSize(), query, searcher); TopDocs topDocs = searcher.searchAfter(after, query, result.getPageSize(), sort); // 设置总记录数 result.setRows(topDocs.totalHits); List<Document> docList = new ArrayList<Document>(); System.out.println(topDocs.totalHits + "topDocs.totalHits......................."); ScoreDoc[] docs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : docs) { int docID = scoreDoc.doc; Document document = searcher.doc(docID); docList.add(document); } result.setDoc_list(docList); searcher.getIndexReader().close(); }
这下面是FieldDoc的源码
public class FieldDoc extends ScoreDoc { /** Expert: The values which are used to sort the referenced document. * The order of these will match the original sort criteria given by a * Sort object. Each Object will have been returned from * the <code>value</code> method corresponding * FieldComparator used to sort this field. * @see Sort * @see IndexSearcher#search(Query,Filter,int,Sort) */ public Object[] fields; /** Expert: Creates one of these objects with empty sort information. */ public FieldDoc(int doc, float score) { super (doc, score); } /** Expert: Creates one of these objects with the given sort information. */ public FieldDoc(int doc, float score, Object[] fields) { super (doc, score); this.fields = fields; } /** Expert: Creates one of these objects with the given sort information. */ public FieldDoc(int doc, float score, Object[] fields, int shardIndex) { super (doc, score, shardIndex); this.fields = fields; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在获取上一页最后一个ScoreDoc方法中,也需要将sort传入。 TopDocs tds = searcher.search(query, num, sort); return tds.scoreDocs[num - 1]; 具体可以看:http://my.oschina.net/sheldon1/blog/395834
TopFieldDocs tds = searcher.search(query, num, sort);
return (FieldDoc) tds.scoreDocs[num - 1];