Query to group by multiple fields

Hello,
Lets us assume that i have customers information with city , state.
Now i want to Group Customer By State, by City.
can anyone help me to form query with the multiple fields?

if it is only for state, the query is as shown below
GET customerindex/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"size": 100,
"field": "state.keyword"
}
}
}
}

But i want to form query to groupby state and city(multiple fields).

Waiting for your response,
thanks in advance.

hope this must works, but i am not getting,
Suppose you want to group by fields field1 , field2 and field3 :

{
  "aggs": {
    "agg1": {
      "terms": {
        "field": "field1"
      },
      "aggs": {
        "agg2": {
          "terms": {
            "field": "field2"
          },
          "aggs": {
            "agg3": {
              "terms": {
                "field": "field3"
              }
            }
          }          
        }
      }
    }
  }
}

Can someone help me please,

May be a concrete example will help. Can you show what is your requirement with example?

This works fine to group by multiple fields(state,city) and calculate sum of the field "total_value",

{
"size":0,
"aggs":{
"group_by_city":{
"terms":{
"field":"city.keyword"
},
"aggs":{
"total_amount_city":{
"sum":{
"field":"total_value"
}
}
}
},
"group_by_state":{
"terms":{
"field":"state.keyword"
},
"aggs":{
"total_amount_state":{
"sum":{
"field":"total_value"
}
}
}
}
}
}

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