Sorting within nested objects. Allow filters on sort operation
Atlas search sorting currently only supports a direct sort on root level fields or nested fields indexed as type "document": https://www.mongodb.com/docs/atlas/atlas-search/sort/
Sorting cannot be done in conjunction with a filter, so a sort operation on an array of objects sorts based on the values of every document in the array. Filtering the objects inside the array and then sorting is not supported
OpenSearch supports sorting with filtering: https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#nested-sorting
Here is an example of how that is done in elastic search and it would be very useful to have an equivalent feature in Atlas Search:
POST /_search
{
"query": {
"nested": {
"path": "parent",
"query": {
"bool": {
"must": {"range": {"parent.age": {"gte": 21}}},
"filter": {
"nested": {
"path": "parent.child",
"query": {"match": {"parent.child.name": "matt"}}
}
}
}
}
}
},
"sort" : [
{
"parent.child.age" : {
"mode" : "min",
"order" : "asc",
"nested": {
"path": "parent",
"filter": {
"range": {"parent.age": {"gte": 21}}
},
"nested": {
"path": "parent.child",
"filter": {
"match": {"parent.child.name": "matt"}
}
}
}
}
}
]
}

-
Douglas commented
While we have the hood open, supporting `sort mode option` would also be fantastic: