Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samyranavela/44a4144813fab3a410632a500ae027ca to your computer and use it in GitHub Desktop.
Save samyranavela/44a4144813fab3a410632a500ae027ca to your computer and use it in GitHub Desktop.
ou can use a top_hits aggregation that groups on the country field, returns 1 doc per group, and orders the docs by the collected date descending: https://stackoverflow.com/questions/29016271/how-to-get-latest-values-for-each-group-with-an-elasticsearch-query
POST /test/_search?search_type=count
{
"aggs": {
"group": {
"terms": {
"field": "country"
},
"aggs": {
"group_docs": {
"top_hits": {
"size": 1,
"sort": [
{
"collected": {
"order": "desc"
}
}
]
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment