Adding virtual sort field before $search stage in Atlas aggregation

Hello,

We have a use case where we need to sort results from the $search stage in Atlas aggregation based on a virtual/computed field.

The requirement is that $search must be the first stage in the pipeline. However, we need to add a new field using $addFields before $search, to compute a value for sorting. So, we would like to know any possibility to do that?

For example, adding an index position field to sort documents matching a given array of IDs first:

[
  {
    $addFields: {
      indexIds: { 
        $indexOfArray: [idsArray, "$_id"]  
      }
    }
  },
  {
    $search: {
      index: "default",
      compound: {...}
      sort: {
        indexIds: 1,  ...others
      }
    } 
  }
]

Hi @Alan_Lam1 ,

There is no way to do this via aggregation today. As you note, $search must be the first stage in a pipeline and does not work with any post-processed data. We are working on a feature, however, that will allow you to enrich your data as it is indexed. When available, you will be able to use $addFields to create synthetic fields as your data in ingested. You could then sort on this synthetic field. This may help here.

Until then, the best option is to create/manage this field application-side.

Seth

Hi @Seth_Payne,

Thank you for providing this information. I have one more question - approximately when will this feature be released? Thank you.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.