I am currently facing an issue with sorting aggregation results on a text field in Elasticsearch. this is my query
{
"size": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"username",
"fullname"
],
"query": "\"ayan\""
}
}
]
}
},
"aggs": {
"total_count": {
"cardinality": {
"field": "userid"
}
},
"group_by_userId": {
"terms": {
"field": "userid",
"size": 10,
"include": {
"partition": 0,
"num_partitions": 2
}
},
"aggs": {
"latest_login": {
"top_hits": {
"_source": {
"includes": [
"username",
"userid",
"logintime",
]
},
"sort": [
{
"logintime": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}
}
}
now i need to sort the group_by_userId bucket with username field or createdtimefield which depends on situation. I'm able to sort it with createdtimefield because it was a datetime field. by adding bucket_sort with max but I could not found a way to do it with the username.