ElasticSearch group by multiple fields
The only close thing that I've found was: Multiple group-by in Elasticsearch Basically I'm trying to get the ES equivalent of the following MySql query: select gender, age_range, count(distinct profile_id) as count FROM TABLE group by age_range, gender The age and gender by themselves were easy to get: { "query": { "match_all": {} }, "facets": { "ages": { "terms": { "field": "age_range", "size": 20 } }, "gender_by_age": { "terms": { "fields": [ "age_range", "gender" ] } } }, "size": 0 } which gives: { "ages": { "_type": "terms", "missing": 0, "total": 193961, "other": 0, "terms": [ { "term": 0