Invalid aggregation order path

Hello everybody!

I am using ElasticSearch 7.6 with a test index that has the following mapping:

{
  "test" : {
    "mappings" : {
      "properties" : {
        "amount" : {
          "type" : "long"
        },
        "group_id" : {
          "type" : "long"
        },
        "id" : {
          "type" : "long"
        }
      }
    }
  }
}

Now, i am using the following query:

{
  "size": 0,
  "track_total_hits": false,
  "aggs": {
    "strategies": {
      "terms": {
        "field": "group_id",
        "order": {
          "drawdown": "asc"
        },
        "size": 100,
        "include": {
          "partition": 1,
          "num_partitions": 100
        }
      },
      "aggs": {
        "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
}

Any workaround?

Hello @Damiano

I see you've already posted on https://github.com/elastic/elasticsearch/issues/8486.

I am wondering if it would be possible to test with composite aggregations.

Another approach would be to use the aggregation you've written in a Transform Job so you will have one document per group_id and you'll be able to do a search over the documents sorting by drawdown.

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