Compute a ratio of one-to-many sum aggregated values

hey, I am trying to compute a ratio of one-to-many sum aggregated values - obtained via a filters aggregation - against a top-level sum aggregated value without luck.

Basically I want to compute a score for all docs (sum) and then for each bucket created:

  • calculate the bucket score :white_check_mark:
  • calculate the bucket ratio: total score / bucket score

Below is the query used and the limitation is related with: bucket_path - Pipeline aggregations | Elasticsearch Guide [7.17] | Elastic because: Paths are relative from the position of the pipeline aggregation; they are not absolute paths, and the path cannot go back "up" the aggregation tree.

{
  "size": 0,
  "aggs": {
    "score_total": {
      "sum": {
        "field": "score"
      }
    },
    "keyword": {
      "filters": { /* apply filters to create buckets - avoid to include because are larger*/ },
      "aggregations": {
        "score_keyword": {
          "sum": {
            "field": "score"
          }
        },
        "ratio_calculated": {
          "bucket_script": {
            "buckets_path": {
              "bucket_value": "score_keyword",
              "total_value": "score_total"
            },
            "script": "params.bucket_value / params.total_value",
            "gap_policy": "insert_zeros"
          }
        }
      }
    }
  }
}

I checked over others pipelines aggregation without success, so I want to know if there is a limitation/constraint from ES or if is possible to make in another way (more/less efficient but at least that works).

Thanks in advance!

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