Sort results by ranking

user6824632 Source

I am trying to search for a keyword in Mongo collection but results not sorted as per their rank..so after finding match, I am sorting by textscore using the code below In java code , I am writing

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

I am getting an error "pipeline requires text score metadata, but there is no text score available"

I have created a text index on the field.. appreciate if you can point me into right direction .

    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;

}

Thanks SK

springmongodbspring-bootmongodb-queryaggregation-framework

Answers

answered 5 days ago Veeram #1

I found the bug report where the validation was added if text score is referenced without text search.

So you require to perform a text search to return the text score. Check your filter to see it includes text search.

ex: {$match: {$text: {$search: search term}}},

answered 3 days ago user6824632 #2

As I have added textscore to projections, only textscore is getting displayed.. commenting out projections displayed all the fields

comments powered by Disqus