# Aggregate by multiple fields of object in array

**URL:** <https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830>\
**Category:** Elasticsearch\
**Created:** [March 20, 2024, 3:02pm UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830 "2024-03-20T15:02:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaustav\_Shouvik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaustav_shouvik/32/132830_2.png) [@Kaustav\_Shouvik](https://discuss.elastic.co/u/Kaustav_Shouvik)\
**Post date:** [March 20, 2024, 3:02pm UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830/1 "2024-03-20T15:02:35Z")

</div>

My data looks like this -

```auto
[
  {
    "orderId": "1",
    "items": [
      {"id": "1", "name": "Item1"},
      {"id": "2", "name": "Item2"}
    ]
  },
  {
    "orderId": "2",
    "items": [
      {"id": "1", "name": "Item1"}
    ]
  }
]

```

I want to aggregate by item id and name.

I am expecting a result like this:

{"id": "1", "name": "Item1"} -\> 2  
{"id": "2", "name": "Item2"} -\> 1

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 20, 2024, 4:24pm UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830/2 "2024-03-20T16:24:42Z")

</div>

Hi @Kaustav_Shouvik

You did not provide your mapping but assuming that your items field is of type nested you can use nested aggs and multi-terms aggs.

I recommend reading this [article](https://opster.com/guides/elasticsearch/search-apis/elasticsearch-aggregation-multiple-fields/).

An example would be this:

```auto
{
  "size": 0,
  "aggs": {
    "agg_item": {
      "nested": {
        "path": "items"
      },
      "aggs": {
        "id_and_name": {
          "multi_terms": {
            "terms": [
              {
                "field": "items.id.keyword"
              },
              {
                "field": "items.name.keyword"
              }
            ]
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Kaustav\_Shouvik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kaustav_shouvik/32/132830_2.png) [@Kaustav\_Shouvik](https://discuss.elastic.co/u/Kaustav_Shouvik)\
**Post date:** [March 21, 2024, 5:02am UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830/3 "2024-03-21T05:02:35Z")

</div>

It returns results also for (id - 1, name - item2), which is not what I want.

I only want two fields:

1. (id - 1, name - item1)
2. (id - 2, name - item2)

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 21, 2024, 12:01pm UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830/4 "2024-03-21T12:01:37Z")

</div>

Are the results incorrect or is the format not what you want? Share your query.

---

<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:** [April 18, 2024, 12:02pm UTC](https://discuss.elastic.co/t/aggregate-by-multiple-fields-of-object-in-array/355830/5 "2024-04-18T12:02:33Z")

</div>

Closed on April 18, 2024, 12:02pm UTC

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