# Sorting based on scripted metrics aggregation

**URL:** <https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384>\
**Category:** Elasticsearch\
**Created:** [January 27, 2021, 3:05pm UTC](https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384 "2021-01-27T15:05:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abbass](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abbass/32/85817_2.png) [@Abbass](https://discuss.elastic.co/u/Abbass)\
**Post date:** [January 27, 2021, 3:05pm UTC](https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384/1 "2021-01-27T15:05:12Z")

</div>

Hello!

I faced a limitation on sorting buckets of a term aggrigation, based on a scripted metrics sub aggregation values.

This [issue](https://github.com/elastic/elasticsearch/issues/8486) clearly states that it is not possible to do this in elasticsearch.

However, i managed to do this using Bucket Sort aggregation.  
Results make sense as far i can tell.  
I want to know am i missing something? is my approach correct?  
Query:

```auto
{
  "aggs": {
    "by-method-name": {
      "terms": {
        "field": "event_name.keyword"
      },
      "aggs": {
        "percentages_calc": {
          "scripted_metric": {
            "init_script": "state.errors = 0; state.t = 0;",
            "map_script": "if (doc['event_outcome.keyword'].value == 'ERROR') { state.errors += 1;state.t += 1} state.t += 1",
            "combine_script": "return state",
            "reduce_script": "double failureT = 0;double total = 0; for (s in states) {failureT += s.errors; total += s.t} return (failureT/total) * 100;"
          }
        },
        "sort_by_percentage": {
          "bucket_sort": {
            "sort": [
              {
                "percentages_calc.value": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  },
  "size": 0
}

```

---

<div class="post-metadata">

**Author:** ![Abbass](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abbass/32/85817_2.png) [@Abbass](https://discuss.elastic.co/u/Abbass)\
**Post date:** [February 2, 2021, 10:24am UTC](https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384/2 "2021-02-02T10:24:14Z")

</div>

One more note is that i am hardcoding a large enough size & shard\_size parameters for terms aggregation to ensure zero doc\_count\_error\_upper\_bound. So the query is actually:

```auto
{
  "aggs": {
    "by-method-name": {
      "terms": {
        "field": "event_name.keyword",
        "size": 5000000,
        "shard_size":5000000
      },
      "aggs": {
        "percentages_calc": {
          "scripted_metric": {
            "init_script": "state.errors = 0; state.t = 0;",
            "map_script": "if (doc['event_outcome.keyword'].value == 'ERROR') { state.errors += 1;state.t += 1} state.t += 1",
            "combine_script": "return state",
            "reduce_script": "double failureT = 0;double total = 0; for (s in states) {failureT += s.errors; total += s.t} return (failureT/total) * 100;"
          }
        },
        "sort_by_percentage": {
          "bucket_sort": {
            "sort": [
              {
                "percentages_calc.value": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  },
  "size": 0
}

```

---

<div class="post-metadata">

**Author:** ![Abbass](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abbass/32/85817_2.png) [@Abbass](https://discuss.elastic.co/u/Abbass)\
**Post date:** [February 23, 2021, 9:45am UTC](https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384/3 "2021-02-23T09:45:25Z")

</div>

Anybody can comment here ? 🙂  
Thanks!!

---

<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:** [March 23, 2021, 9:45am UTC](https://discuss.elastic.co/t/sorting-based-on-scripted-metrics-aggregation/262384/4 "2021-03-23T09:45:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
