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

**URL:** https://discuss.elastic.co/t/compute-a-ratio-of-one-to-many-sum-aggregated-values/323058
**Category:** Elasticsearch
**Created:** [January 12, 2023, 8:12pm UTC](https://discuss.elastic.co/t/compute-a-ratio-of-one-to-many-sum-aggregated-values/323058 "2023-01-12T20:12:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![fzar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fzar/32/86117_2.png) [@fzar](https://discuss.elastic.co/u/fzar)
#### Post date: [January 12, 2023, 8:12pm UTC](https://discuss.elastic.co/t/compute-a-ratio-of-one-to-many-sum-aggregated-values/323058/1 "2023-01-12T20:12:21Z")

</div>

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 ✅
- 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](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-pipeline.html#buckets-path-syntax) 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.`

```auto
{
  "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!

---

<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: [February 9, 2023, 8:12pm UTC](https://discuss.elastic.co/t/compute-a-ratio-of-one-to-many-sum-aggregated-values/323058/2 "2023-02-09T20:12:27Z")

</div>

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