# Reverse nested problem

**URL:** https://discuss.elastic.co/t/reverse-nested-problem/334783
**Category:** Elasticsearch
**Created:** [May 31, 2023, 10:03am UTC](https://discuss.elastic.co/t/reverse-nested-problem/334783 "2023-05-31T10:03:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kenan\_Seyidov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kenan_seyidov/32/118633_2.png) [@Kenan\_Seyidov](https://discuss.elastic.co/u/Kenan_Seyidov)
#### Post date: [May 31, 2023, 10:03am UTC](https://discuss.elastic.co/t/reverse-nested-problem/334783/1 "2023-05-31T10:03:29Z")

</div>

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

```auto
{
    "_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
}

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 1, 2023, 8:46am UTC](https://discuss.elastic.co/t/reverse-nested-problem/334783/3 "2023-06-01T08:46:05Z")

</div>

Please be patient in waiting for responses to your question and refrain from pinging multiple times asking for a response or opening multiple topics for the same question. This is a community forum, it may take time for someone to reply to your question. For more information please refer to the [Community Code of Conduct](https://www.elastic.co/community/codeofconduct) specifically the section "Be patient". Also, please refrain from pinging folks directly, this is a forum and anyone that participates might be able to assist you.

If you are in need of a service with an SLA that covers response times for questions then you may want to consider talking to us about a [subscription](https://www.elastic.co/subscriptions).

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 29, 2023, 8:46am UTC](https://discuss.elastic.co/t/reverse-nested-problem/334783/4 "2023-06-29T08:46:27Z")

</div>

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