# How to dynamically aggregate the fields of a document?

**URL:** <https://discuss.elastic.co/t/how-to-dynamically-aggregate-the-fields-of-a-document/62933>\
**Category:** Elasticsearch\
**Created:** [October 13, 2016, 12:01pm UTC](https://discuss.elastic.co/t/how-to-dynamically-aggregate-the-fields-of-a-document/62933 "2016-10-13T12:01:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![psilos](https://avatars.discourse-cdn.com/v4/letter/p/c0e974/32.png) [@psilos](https://discuss.elastic.co/u/psilos)\
**Post date:** [October 13, 2016, 12:01pm UTC](https://discuss.elastic.co/t/how-to-dynamically-aggregate-the-fields-of-a-document/62933/1 "2016-10-13T12:01:28Z")

</div>

I have an Elasticsearch instance where I store products with the following schema:

```
    "productId": "37807",
    "name": "trendy t-shirt",
    "price": "72.59",
    "customFilters": {
      "color": "red",
      "size": "large",
    }

```

Depending on the category of the product, there might be different customFilters. E.g. if it is a mobile phone, then we may have "screen size", "ram" etc.

```
    "productId": "37808",
    "name": "mobile phone",
    "price": "100.59",
    "customFilters": {
      "screen_size": "5",
      "ram": "3",
    }

```

What I need, is to retrieve all the aggregations for the "customFilters" without explicitly specifying the name of the filters. So in the first case I need to get the aggregation for "color" and "size" whereas in the second one for "screen\_size" and "ram".

Any ideas? Thnx

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 14, 2016, 8:52am UTC](https://discuss.elastic.co/t/how-to-dynamically-aggregate-the-fields-of-a-document/62933/2 "2016-10-14T08:52:22Z")

</div>

you can use a template aggregation!

---

<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:** [July 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/how-to-dynamically-aggregate-the-fields-of-a-document/62933/3 "2017-07-05T22:12:22Z")

</div>


