请问用Lucene searchafter分页,怎样排序?

发布于 2021-11-28 00:48:40 字数 3888 浏览 868 评论 2

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2021-11-30 00:54:08

在获取上一页最后一个ScoreDoc方法中,也需要将sort传入。 TopDocs tds = searcher.search(query, num, sort); return tds.scoreDocs[num - 1]; 具体可以看:http://my.oschina.net/sheldon1/blog/395834

海之角 2021-11-29 22:56:31

TopFieldDocs tds = searcher.search(query, num, sort);

return (FieldDoc) tds.scoreDocs[num - 1];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文