# Elastic Aggregations query

**URL:** https://discuss.elastic.co/t/elastic-aggregations-query/329807
**Category:** Elasticsearch
**Created:** [April 12, 2023, 7:50am UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807 "2023-04-12T07:50:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![math1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@math1](https://discuss.elastic.co/u/math1)
#### Post date: [April 12, 2023, 7:50am UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807/1 "2023-04-12T07:50:45Z")

</div>

```auto
POST test/_doc/
{
  "food": [
    {
      "food1": {
        "type": "Western food",
        "name": "hamburger"
      },
      "food2": {
        "type": "Japanese food",
        "name": "sushi"
      }
    }
  ]
}

POST test/_doc/
{
  "food": [
    {
      "food1": {
        "type": "Chinese food",
        "name": "jajangmyeon"
      },
      "food2": {
        "type": "Western food",
        "name": "steak"
      }
    }
  ]
}

```

This code is my sample code. I want to create a query that counts the number of fields food1 and food2 by 'type' field. I want the output like the code below.

```auto
        {
          "key": "Western food",
          "doc_count": 2
        },
        {
          "key": "Chinese food",
          "doc_count": 1
        },
        {
          "key": "Japanese food",
          "doc_count": 1
        }

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 12, 2023, 12:19pm UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807/2 "2023-04-12T12:19:31Z")

</div>

You need to use a nested data type for the array and use nested aggregation to compute the numbers.

---

<div class="post-metadata">

### Author: ![math1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@math1](https://discuss.elastic.co/u/math1)
#### Post date: [April 12, 2023, 12:53pm UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807/3 "2023-04-12T12:53:46Z")

</div>

@dadoonet How can I write the code?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 12, 2023, 2:33pm UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807/4 "2023-04-12T14:33:39Z")

</div>

See [Nested aggregation | Elasticsearch Guide [8.7] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)

There's an example of this.

---

<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: [May 10, 2023, 2:34pm UTC](https://discuss.elastic.co/t/elastic-aggregations-query/329807/5 "2023-05-10T14:34:33Z")

</div>

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