代码之家  ›  专栏  ›  技术社区  ›  user6824632

按排名对结果排序

  •  2
  • user6824632  · 技术社区  · 7 年前

    我试图在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公司

    1 回复  |  直到 7 年前
        1
  •  0
  •   user6824632    7 年前

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