Say I have an Elasticsearch index with bunch of users' comments:
{ "name": "chris", "date": "2016-01-01", "msg": "hi, foo"}
{ "name": "chris", "date": "2016-01-05", "msg": "bye, bar"}
{ "name": "aaron", "date": "2016-01-10", "msg": "who's bar"}
{ "name": "aaron", "date": "2016-01-15", "msg": "not foo"}
First, I want to find the lastest comment for each user. I can do that with the top_hits
aggregation:
"aggs": {
"name": {
"terms": { "field": "name" },
"aggs": {
"latest_comment": {
"top_hits": {
"sort": [ {"date": { "order": "desc" } } ],
"size": 1
}
}
}
}
}
}
Which effectively gives me the following:
{ "name": "chris", "date": "2016-01-05", "msg": "bye, bar"}
{ "name": "aaron", "date": "2016-01-15", "msg": "not foo"}
But how can I filter those results now?? And to be super clear, I want to filter after the top_hits
aggregation has picked the latest hits, not before.
Thank you.