Hi
I have a group of user access data which contain the user name, access timestamp, and some of other info. There will be multiple records with the same username but different access timestamp. What I want is to do is to find the latest access records of all user, sort the result by latest access timestamp, and paginate the result.
In general what I need is:
- Aggregate by user name
- Sort the aggregate result by latest access timestamp
- Pagnated the result.
I tried the wizard and it told me to use the terms aggregation. And here is my search script
{
"size": 0,
"aggs": {
"group_by_name": {
"terms": {
"field": "index.name.keyword",
"include": {
"partition": 0,
"num_partitions": 3
},
"size": 132,
"order": {
"maxField": "desc"
}
},
"aggs": {
"latest": {
"top_hits": {
"_source": {
"includes": [
"index.name",
"index.record_created_timestamp"
]
},
"sort": [
{
"index.record_created_timestamp": {
"order": "desc"
}
}
],
"size": 1
}
},
"maxField": {
"max": {
"field": "index.record_created_timestamp"
}
}
}
}
}
}
The script above manages to paginate the result through partitions, but the result is only sorted within each bucket. I didn't manage to sort the result across partitions.
I also tried composite aggregation, but it only supports sorting the results by grouped key(which is username in my case). I didn't find a way to sort through timestamp with composite aggregation.
So are there any other solution for this problem?