# Aggregation

**URL:** <https://discuss.elastic.co/t/aggregation/260038>\
**Category:** Elasticsearch\
**Created:** [January 3, 2021, 1:24pm UTC](https://discuss.elastic.co/t/aggregation/260038 "2021-01-03T13:24:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uri\_Parush](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/uri_parush/32/81667_2.png) [@Uri\_Parush](https://discuss.elastic.co/u/Uri_Parush)\
**Post date:** [January 3, 2021, 1:24pm UTC](https://discuss.elastic.co/t/aggregation/260038/1 "2021-01-03T13:24:48Z")

</div>

Hi,  
I'm trying to understand how to create an aggregation query for tag distribution.

Record example:

```auto
{
 @timestamp: {iso time} ,
 record_type: {string},
 record_data: {string},
 tags: [{key: {string}, value: {string}}, ...]
}

```

I want to query the data and get a distribution of tags (key, value) over a specific record type.  
Example of response:

```auto
{
"record_type_1":
   [
       {key: "key1",
        value: "value1",
        count: 2
       },
       {key: "key2",
        value: "value2",
        count: 1
       }
   ]
}

```

I'm not sure which aggregation pipeline should I use?

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [January 3, 2021, 6:06pm UTC](https://discuss.elastic.co/t/aggregation/260038/2 "2021-01-03T18:06:18Z")

</div>

if `tags` field is mapped as nested type, then you need to check the bellow link

> **[Nested aggregation | Elasticsearch Reference \[7.10\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)**

---

<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:** [January 31, 2021, 6:06pm UTC](https://discuss.elastic.co/t/aggregation/260038/3 "2021-01-31T18:06:23Z")

</div>

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