Search sorting by searchScore without $project?

When I search a certain query and have lots of results with the same search score, I’ve noticed that each time I run the query, the default sorting of results tend to change each time I run the query.

My concern is that when I do pagination with $limit & $skip I may not get expected results because the sort order is changing for subsequent $skip queries.

If I could $sort on the searchScore first, then _id second (or created date or whatever), I could solve that issue in a simple way.

The examples I’ve seen require you to $project the $meta.searchScore before you can $sort on it.

The problem with that is then I need to specify all the other fields I want to $project. I want to include all the other fields without specifying them because they could change and I don’t want to have to remember to update the search code whenever another field is added to the collection.

Is there a way I can tell $project to include all fields, or, is there a way to $sort on searchScore without using the $project stage?

I found the solution to my issue in the docs. :slight_smile:

"$sort": {
  "score": { "$meta": "textScore" },
  "created": -1
}
1 Like

Actually, this isn’t working as expected. There seems to be a bug. Why does it not order by searchScore? Is there a difference between textScore and searchScore?

Hi @djedi,

Actually, this isn’t working as expected. There seems to be a bug. Why does it not order by searchScore? Is there a difference between textScore and searchScore ?

Could you provide a few sample documents and the full pipeline used? I would like to perform some testing on my own environment to see if I’m able to replicate the same results.

Additionally, could you confirm the MongoDB version in use that had produced the results you have shown?

Update : I tried to change searchScore to textScore for my own test environment but did not receive any output for a projection of textScore in a $search pipeline.

Thanks in advance,
Jason

Hi @djedi,

Just providing a quick update for this post.

Why does it not order by searchScore?

Regarding searchScore, documents in the result set are returned in order from highest score to lowest for Atlas Search by default so you don’t need to include an addition descending $sort on the score. The screenshot is cut out but my guess is that the first stage shown in that screenshot is a $project for the searchScore but let me know if this is not the case. If so, it does appear that the results are appearing from highest to lowest score as expected.

Is there a difference between textScore and searchScore?

textScore is related to native text search where as searchScore is used in Atlas Search.

Regards,
Jason

1 Like