Skip to content

Unable to sort by result of bucket script aggregation #32153

@ydzhu

Description

@ydzhu

I get result by using bucket script aggregation, but I can't sort by this aggregation value. For explame,

{
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {
    "api_terms": {
      "terms": {
        "field": "name",
        "order": {
          "avg_time": "desc"
        }
      },
      "aggs": {
        "sum_duration": {
          "sum": {
            "field": "duration"
          }
        },
        "sum_count": {
          "sum": {
            "field": "count"
          }
        },
        "avg_time": {
          "bucket_script": {
            "buckets_path": {
              "duration": "sum_duration",
              "count": "sum_count"
            },
            "script": "params.duration / params.count"
          }
        }
      }
    }
  }
}

I hope sort by "avg_time" that is calculated by bucket_script, so I add order in term aggregation ( "order": {"avg_time": "desc"}). But it cause error. This reason of error is “Invalid aggregator order path [avg_time]. Unknown aggregation [avg_time]".
Even more puzzling is I use add order by sum_count instead of avg_time, i can get correct value.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions