I am trying to get a list of users who has as their last activity "connect". Ideally, I want this as a metric viz or a data table in Kibana showing the number of users that connected last and the list of them, respectively. I have, however, given up being able to do this in Kibana. I can get something similar directly from Elasticsearch using a terms aggregation followed by top_hits as below. But the problem is, even though I am sorting the top_hits by @timestamp, the resulting document in NOT the most recent.
{
"size" : 0,
"sort": { "@timestamp": {"order": "desc"} },
"aggs" : {
"by_user" : {
"terms" : {
"field" : "fields.username.keyword",
"size" : 1
},
"aggs": {
"last_message": {
"top_hits": {
"sort": [
{
"@timestamp": {
"order": "desc"
}
}
],
"_source": {
"includes": ["fields.username.keyword", "@timestamp", "status"]
},
"size": 1
}
}
}
}
}
}
- Is there a way to do this directly in Kibana?
- How can I make sure top_hits gives me the latest results, rather than the "most relevant"?