Reverse nested problem

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
}
1 Like

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 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.

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

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