4

I run the following aggregation pipeline and it works well:

[
  {
    $search: {
      text: {
        query: text,
        path: 'name',
        fuzzy: {
          maxEdits: 2,
          prefixLength: 0,
          maxExpansions: 256,
        },
      },
    },
  },
  {
    $limit: 10,
  },
  {
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' },
     },
  },
]

I can see that the score-field is present in my result and is correct. Now I want to sort the documents retrieved by the $meta-property searchScore. However, adding this step at the end of the pipeline:

{
  $sort: { score: { $meta: 'searchScore' }, _id: 1 },
}

Yields the following error:

MongoError: $meta sort by 'searchScore' metadata is not supported

How do I achieve this?

Doug
  • 14,387
  • 17
  • 74
  • 104
Amygdaloideum
  • 3,683
  • 2
  • 13
  • 16
  • 5
    Atlas Search sorts by score by default. Adding a separate $sort stage will cause all of your data to be materialized and slow down your performance considerably. https://docs.atlas.mongodb.com/reference/atlas-search/scoring/ – Doug Mar 25 '21 at 16:55
  • 1
    Also this should help with performance https://docs.atlas.mongodb.com/reference/atlas-search/performance/#-sort-aggregation-stage-usage – Doug Mar 25 '21 at 17:01
  • My pipeline was more complicated IRL. I have a form in my UI where the search input is optional. And I need different sorting based on if the query contained a search stage or not. However, now I understand that I probably should divide these into two queries for performance that leverages the default sorting of the text search. Your input was very appreciated! – Amygdaloideum Mar 26 '21 at 09:39

1 Answers1

1

You need to add a new field that contains the result of $meta before sorting (which you already do) and then sort on that field, so you new pipeline would be:

...

{
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' }, // you are already adding the field here.
     },
},
{
   $sort: {
      score: -1, // use the new computed field here.
      _id: 1
   }
}
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43