# Terms aggregation bucket sort order based on scripted metric aggregation

**URL:** <https://discuss.elastic.co/t/terms-aggregation-bucket-sort-order-based-on-scripted-metric-aggregation/426>\
**Category:** Elasticsearch\
**Created:** [May 8, 2015, 10:54pm UTC](https://discuss.elastic.co/t/terms-aggregation-bucket-sort-order-based-on-scripted-metric-aggregation/426 "2015-05-08T22:54:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vb3](https://avatars.discourse-cdn.com/v4/letter/v/7993a0/32.png) [@vb3](https://discuss.elastic.co/u/vb3)\
**Post date:** [May 8, 2015, 10:54pm UTC](https://discuss.elastic.co/t/terms-aggregation-bucket-sort-order-based-on-scripted-metric-aggregation/426/1 "2015-05-08T22:54:32Z")

</div>

Similar to this question's solution: [http://stackoverflow.com/questions/28320062/elasticsearch-aggregation-bucket-sort-order](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.

---

<div class="post-metadata">

**Author:** ![vb3](https://avatars.discourse-cdn.com/v4/letter/v/7993a0/32.png) [@vb3](https://discuss.elastic.co/u/vb3)\
**Post date:** [May 8, 2015, 11:28pm UTC](https://discuss.elastic.co/t/terms-aggregation-bucket-sort-order-based-on-scripted-metric-aggregation/426/2 "2015-05-08T23:28:22Z")

</div>

Looks like it's being tracked here:

> <https://github.com/elastic/elasticsearch/issues/8486>
>
> Since there is a new Scripted metric aggregation (scripted\_metric) in 1.4, it is possible to do a lot of amazing stuff.
> For...

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:14am UTC](https://discuss.elastic.co/t/terms-aggregation-bucket-sort-order-based-on-scripted-metric-aggregation/426/3 "2017-07-06T00:14:55Z")

</div>


