【问题标题】:Sort results by ranking按排名对结果进行排序
【发布时间】:2018-07-25 11:53:27
【问题描述】:

我正在尝试在 Mongo 集合中搜索关键字,但结果未按其排名排序..所以在找到匹配项后,我使用下面的代码按 textscore 排序 在java代码中,我正在写

Aggregates.project(Projections.metaTextScore("textScore"))
Aggregates.sort(Sorts.metaTextScore("textScore"))

我收到错误消息“管道需要文本分数元数据,但没有可用的文本分数”

我已经在字段上创建了一个文本索引。如果您能指出我正确的方向,不胜感激。

    public Document find(Map<String,String> params){    

    ProcessRequest request  = new ProcessRequest();
    request.parseSearch(params);
    MongoCollection<Document> mcollection = mconfig.getRecordCollection();
    long count  = mcollection.count(request.getFilter());
    logger.info("Count :"+count);
    AggregateIterable<Document> aggre = null;
    try{
     aggre = mcollection.aggregate(request.getQueryList());
    }
    catch(Exception e){
        logger.info(e.getMessage());
    }
    Document resultDoc = new Document();
    resultDoc.put("ResultCount", count);
    resultDoc.put("PageSize", request.getPageSize());
    resultDoc.put("ResultData",aggre);
    return resultDoc;

}

谢谢 SK

【问题讨论】:

  • 你能告诉我们整个聚合吗?
  • @Veeram 我已经用更多细节更新了问题
  • 没有看到任何文本搜索代码。你有更多的代码吗?你的 spring mongo jar 版本是什么?
  • spring mongo jar 是 3.4.2

标签: spring mongodb spring-boot mongodb-query aggregation-framework


【解决方案1】:

由于我已将 textscore 添加到投影中,因此仅显示 textscore.. 注释掉显示所有字段的投影

【讨论】:

  • 只是好奇。这不是和你昨天遇到的不同的问题吗?您是如何解决问题中提到的错误的?
  • @veeram 我收到管道错误,因为搜索文本值为空。所以我设置了一个条件来执行聚合排序 Aggregates.sort(Sorts.metaTextScore("textScore")) 如果有一个搜索值
猜你喜欢
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多