# How to aggregate based on the number of items in a field?

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-based-on-the-number-of-items-in-a-field/282650>\
**Category:** Elasticsearch\
**Created:** [August 27, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-the-number-of-items-in-a-field/282650 "2021-08-27T09:31:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xi\_Ern\_Tee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xi_ern_tee/32/93816_2.png) [@Xi\_Ern\_Tee](https://discuss.elastic.co/u/Xi_Ern_Tee)\
**Post date:** [August 27, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-the-number-of-items-in-a-field/282650/1 "2021-08-27T09:31:19Z")

</div>

On Elasticsearch I have a field named `Itinerary` that can contain multiple values (from 1 up to 6), for example in the picture below there's 2 items in the field.

 ![12333](https://us1.discourse-cdn.com/elastic/original/3X/4/a/4a636ee656ab7d22af16803e94db20e44cca6448.png)  
Is there a way I can aggregate based on the number of items in the field? Having something like:

```auto
1 item : 2
2 item : 4
...

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 30, 2021, 11:25am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-the-number-of-items-in-a-field/282650/2 "2021-08-30T11:25:00Z")

</div>

I would go with an [ingest processor](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/ingest-processors.html) that calculates the size of the number of fields before indexing. This way, querying stays fast.

If you just want to test this feature, try a [runtime field](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/runtime.html) doing that calculation, so that you can filter on it - and even replace it with an indexed field later without replacing your query.

Hope this helps!

---

<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:** [September 27, 2021, 11:25am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-the-number-of-items-in-a-field/282650/3 "2021-09-27T11:25:20Z")

</div>

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