Elastic Aggregations query

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.

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

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

@dadoonet How can I write the code?

See Nested aggregation | Elasticsearch Guide [8.7] | Elastic

There's an example of this.

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