# Counting each key of an array inside aggregation

**URL:** <https://discuss.elastic.co/t/counting-each-key-of-an-array-inside-aggregation/296060>\
**Category:** Elasticsearch\
**Created:** [February 2, 2022, 11:36am UTC](https://discuss.elastic.co/t/counting-each-key-of-an-array-inside-aggregation/296060 "2022-02-02T11:36:39Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![hugomeloo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hugomeloo/32/101228_2.png) [@hugomeloo](https://discuss.elastic.co/u/hugomeloo)\
**Post date:** [February 3, 2022, 3:06pm UTC](https://discuss.elastic.co/t/counting-each-key-of-an-array-inside-aggregation/296060/5 "2022-02-03T15:06:41Z")

</div>

The generation of this map is not a problem for me as I can change the structure of the field to whatever I want. The problem is the query with script which ran over a very big dataset can be too expensive.  
I could also create this field with information structured like:

```auto
{ "room": 2, "restroom": 1 }

```

or

```auto
{"topic": "room", "count": 2}, {"topic": "restroom", "count": 1 }

```

Thanks for helping!

---

_[View the full topic](https://discuss.elastic.co/t/counting-each-key-of-an-array-inside-aggregation/296060)._
