Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams "drawdown": { "scripted_metric": { "init_script": "state.id = 0; state.cumsum = 0; state.max = 0; state.min = 0; state.diff = 0", "map_script": "state.cumsum += doc.amount.value; if (state.cumsum>state.max) {state.max=state.cumsum;state.id=doc.id.value} if (state.max - state.cumsum >= state.diff) {state.min=state.cumsum;state.diff=state.max - state.cumsum;}", "combine_script": "return state;", "reduce_script": "states.sort((x, y) -> x.id - y.id); int min = states[0].min; int max = states[0].max; for(int i = 1; i < states.length; i++) { int nextMin = states[i].min; int nextMax = states[i].max; if (max > nextMax && min > nextMin) { min = nextMin; } else if (max < nextMax && max-min < nextMax-nextMin) { max = nextMax; min = nextMin;} } return max-min;"

If i remove the order part the query works good, i am referring to the following piece:

"order": {"drawdown": "asc"}

How can i sort by the value returned by drawdown aggregation?

The error that i get is:

"error": { "root_cause": [ "type": "aggregation_execution_exception", "reason": "Invalid aggregation order path [drawdown]. 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." "type": "search_phase_execution_exception", "reason": "all shards failed", "phase": "query", "grouped": true, "failed_shards": [ "shard": 0, "index": "test", "node": "jHJ3_eOLQ3iZZeHZPI_dFA", "reason": { "type": "aggregation_execution_exception", "reason": "Invalid aggregation order path [drawdown]. 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." "status": 500

We can order terms aggs by single or multi value metrics sub-aggregations, i.e.:

GET test/_search
  "size": 0,
  "track_total_hits": false,
  "aggs": {
    "strategies": {
      "terms": {
        "field": "group_id",
        "size": 100,
        "order": {
          "stats_agg.max": "desc"
      "aggs": {
        "stats_agg": {
          "stats": {
            "field": "amount"

TBH I also cannot understand why a single-value scripted metric is not considered a valid aggregation sorter -.-

i do not know, maybe for example copying somehow the value of the drawdown agg. into annother and then sort it. Utopian? – Dail Apr 17, 2020 at 12:39 No you can't. Go thru the github comments -- somewhere in there are reasons for that. Don't exactly remember what they are tho. – Joe - ElasticsearchBook.com Apr 17, 2020 at 12:40

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.