Terms aggregation bucket sort order based on scripted metric aggregation

Similar to this question's solution: http://stackoverflow.com/questions/28320062/elasticsearch-aggregation-bucket-sort-order

I am trying to do the following query:
{
"aggs": {
"agg1": {
"terms": {
"field": "some_field_name",
"order": {
"poc_rate": "asc"
}
},
"aggs": {
"poc_rate": {
"scripted_metric": {
"init_script_file": "poc_rate_init",
"map_script_file": "poc_rate_map",
"reduce_script_file": "poc_rate_reduce"
}
}
}
}
},
"size": 0
}

However, I am getting error:
SearchPhaseExecutionException[Failed to
execute phase [query_fetch], all shards failed; shardFailures
{[dsafasdfadsfas][some_index][0]:
RemoteTransportException[[elasticsearch-beta][inet[/234523452:9300]][indices:data/read/search[phase/query+fetch]]];
nested: AggregationExecutionException[Invalid terms aggregation order
path [poc_rate]. Terms buckets can only be sorted on a sub-aggregator
path that is built out of zero or more single-bucket aggregations within
the path and a final single-bucket or a metrics aggregation at the path
end.]; }]

I have verified that the scripted_metric it self works, and it returns double values.

Please help on why terms agg cannot be sorted by scripted_metric sub agg. thank you.

Looks like it's being tracked here:

If this could be picked up, that would be great!

1 Like