Lakhan Jain
Lakhan Jain

Reputation: 161

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

Upvotes: 3

Views: 541

Answers (2)

cmhteixeira
cmhteixeira

Reputation: 967

What you want is called aggregations. Here is the query:

GET /yourIndex/_search
{
  "aggs": {
    "myCustomAggregation": {
      "terms": {
        "field": "field1"
      }
    }
  }
}

Note however that you cannot perform aggregations on fields (in your case field field1) which are text fields. You should set that field as keyword.

Upvotes: 1

Val
Val

Reputation: 217274

You can simply use a terms aggregation:

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

Upvotes: 1

Related Questions