In Elasticsearch we use a nested query, when we use the reverse nesting it does not remember the previous aggregation, it only returns the topmost eligible documents.
For example : In the following query, rate_option, service_name objects are nested objects and objects like branch, id, feedback_count are parent objects. The query up to branch_reverse works fine but objects after rate_nested return all parent objects
Elasticsearch version: 8.6.1
{
"_source": false,
"query": {
"bool": {
"filter": [
{
"terms": {
"id": [
218455
]
}
}
]
}
},
"aggs": {
"feedback_terms": {
"terms": {
"field": "id",
"size": 10712
},
"aggs": {
"service_nested": {
"nested": {
"path": "feedback_rate"
},
"aggs": {
"service_terms": {
"terms": {
"field": "feedback_rate.service.name.keyword",
"size": 200,
"order": {
"_key": "asc"
}
},
"aggs": {
"branch_reverse": {
"reverse_nested": {},
"aggs": {
"branch_terms": {
"terms": {
"field": "branch.name.keyword",
"size": 100,
"order": {
"_key": "asc"
}
},
"aggs": {
"rate_nested": {
"nested": {
"path": "feedback_rate"
},
"aggs": {
"rate_terms": {
"terms": {
"field": "feedback_rate.rate_option",
"script": {
"params": {
"rateOption": {
"1": "Excellent",
"2": "Good",
"3": "Neutral",
"4": "Bad",
"5": "Unacceptable"
}
},
"source": "def rate =params.rateOption; def value = doc['feedback_rate.rate_option'].value.toString(); return rate.containsKey(value) ? rate[value] : value"
},
"value_type": "string"
},
"aggs": {
"rate_count_metric": {
"value_count": {
"field": "feedback_rate.rate_option"
}
},
"feedback_count_reverse": {
"reverse_nested": {},
"aggs": {
"feedback_count_metric": {
"value_count": {
"field": "id"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
"sort": [
{
"_doc": {
"order": "asc"
}
}
],
"track_total_hits": -1
}