As far as I\'m aware, there isn\'t a way to do something like the following in Elasticsearch:
SELECT * FROM myindex
GROUP BY agg_field1, agg_field2, agg_fiel
Field collapsing is the answer.
Field collapsing feature is used when we want to group the hits on a specific field (as in group by agg_field).
Before Elastic 6, the way to group the fields is to use aggregation. This approach was lacking an ability to do efficient paging.
But now, with the field collapse provided out of the box by elastic, it is pretty easy.
Below is a sample query with field collapse taken from above link.
GET /twitter/_search
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "user",
"inner_hits": {
"name": "last_tweets",
"size": 5,
"sort": [{ "date": "asc" }]
},
"max_concurrent_group_searches": 4
},
"sort": ["likes"]
}