Search code examples
elasticsearchluceneelastic-stack

Elastic search 6.2 group by query


elastic search version: 6.2

I want the count data group by a particular field

example:

Assume I have 3 following docs

{ field1: "value1" } { field1: "value2" } { field1: "value2" }

I want the following result

value1: 1

value2: 2

Note: Field is of type text


Solution

  • You can simply use a terms aggregation:

    {
      "size": 0,
      "aggs": {
        "groups": {
          "terms": {
            "field": "field1"
          }
        }
      }
    }