I have a requirement where in I would need to sort the parent bucket ('d1' here) based on the values calculated in an innermost bucket ('percentage' key within udate[0]). Please find my Elasticsearch query below. Thank you in advance.!
{
"query": {
"bool": {
"must": [{
"range": {
"date": {
"gte": "27/10/2018 00:00:00"
}
}
}]
}
},
"aggs": {
"d1": {
"terms": {
"field": "d.keyword",
"size": 100
},
"aggs": {
"udate": {
"terms": {
"field": "date",
"size": 10
},
"aggs": {
"value_count": {
"value_count": {
"field": "status.keyword"
}
},
"filterStatus": {
"filter": {
"prefix": {
"status": "4"
}
},
"aggs": {
"error_count": {
"value_count": {
"field": "status.keyword"
}
}
}
},
"percentage": {
"bucket_script": {
"buckets_path": {
"errorCount": "filterStatus>error_count",
"totalCount": "value_count"
},
"script": "params.errorCount/params.totalCount * 100"
}
}
}
}
}
}
},
"size": 0
}