# Problem with aggregation returning most popular array entry among all documents

**URL:** <https://discuss.elastic.co/t/problem-with-aggregation-returning-most-popular-array-entry-among-all-documents/319127>\
**Category:** Elasticsearch\
**Created:** [November 16, 2022, 8:52pm UTC](https://discuss.elastic.co/t/problem-with-aggregation-returning-most-popular-array-entry-among-all-documents/319127 "2022-11-16T20:52:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karol\_Radomski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karol_radomski/32/97648_2.png) [@Karol\_Radomski](https://discuss.elastic.co/u/Karol_Radomski)\
**Post date:** [November 16, 2022, 8:52pm UTC](https://discuss.elastic.co/t/problem-with-aggregation-returning-most-popular-array-entry-among-all-documents/319127/1 "2022-11-16T20:52:20Z")

</div>

Please help me solve aggregation issue:

Lets say I have mapping

```auto
(...)
"properties": {
    "kidName": {
        "type": "keyword",
        "fields": {
            "txt": {
                "type": "text",
            }
        }
    },
    "eatenFruit": {
        "type": "nested",
        "properties": {
            "fruitName": {
                "type": "keyword",
                "fields": {
                    "txt": {
                        "type": "text"
                    }
                }
            }
        }
    }
}
(...)

```

And want to achieve query that will give me 20 most popular fruits among all children.

I've came up with something like this:

```auto
(...)
"aggs": {
        "ListOfMostPopularFruits": {
            "nested": {
                "path": "eatenFruit"
            },
            "aggs": {
                "Sorted": {
                    "terms": {
                        "field": "eatenFruit.fruitName",
                        "size": 20,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    }
                }
            }
        }
    }
(...)

```

but this is not what I want. Because if let's say Billy has eaten 20 apples, Mandy has eaten 2 bananas and Johny has eaten 5 bananas. This will return:

```auto
(...)
"aggregations": {
        "ListOfMostPopularFruits": {
            "doc_count": 123456,
            "Sorted": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 123,
                "buckets": [
                    {
                        "key": "apple",
                        "doc_count": 20
                    },
                    {
                        "key": "banana",
                        "doc_count": 7
                    }
                ]
            }
        }
    }
(...)

```

and I need aggregation that will give me something like:

```auto
(...)
"aggregations": {
        "ListOfMostPopularFruits": {
            "doc_count": 123456,
            "Sorted": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 123,
                "buckets": [
                    {
                        "key": "banana",
                        "doc_count": 2
                    },
                    {
                        "key": "apple",
                        "doc_count": 1
                    }
                ]
            }
        }
    }
(...)

```

My limitations:

I'm limited to version 7.10 and I cannot denormalize data more than it is (the only thing I can do is get rid of nested type).

Can any1 help me with solving this issue?

---

<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:** [December 14, 2022, 8:53pm UTC](https://discuss.elastic.co/t/problem-with-aggregation-returning-most-popular-array-entry-among-all-documents/319127/2 "2022-12-14T20:53:19Z")

</div>

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