# Multiple sum aggregations using the same script

**URL:** <https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938>\
**Category:** Elasticsearch\
**Created:** [December 4, 2016, 12:38am UTC](https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938 "2016-12-04T00:38:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asafo](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@asafo](https://discuss.elastic.co/u/asafo)\
**Post date:** [December 4, 2016, 12:38am UTC](https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938/1 "2016-12-04T00:38:20Z")

</div>

Hi, could use some advice here:

I need to calculate the weighted sums of sets of documents. The weight is based on the fields of the document so I'm using scripts, in a particular a sum aggregation with a script inside filter aggregations. The weights are fixed per call (i.e. all the sum aggregations use the exact same script and pararms).

I have implemented this but I'm not completely satisfied with the solution for 2 reasons:

1. The script runs multiple times for the same document (for every filter the document matches).
2. I need to pass the parameters for the script multiple times. This param object could get quite big, and I don't want to risk hitting ES max request size.

Anything I could do differently?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 5, 2016, 9:09am UTC](https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938/2 "2016-12-05T09:09:54Z")

</div>

Hey,

I think it would help greatly if you shared the requests you created, so people could get a feeling what you are after.

Also, do you think it is possible to precalculate anything of this before storing the document, so you could add it to the document itself to be faster on the query side?

--Alex

---

<div class="post-metadata">

**Author:** ![asafo](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@asafo](https://discuss.elastic.co/u/asafo)\
**Post date:** [December 6, 2016, 7:28pm UTC](https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938/3 "2016-12-06T19:28:34Z")

</div>

Thanks for the reply @spinscale .

I've simplified the search request as my mapping is a bit more complex, but it boils down to this:

```
{
  "size": 0,
  "query": {
    "filtered": {
      "filter": {
        
        ... global filter 
        
      }
    }
  },
  "aggs": {
    "agg1": {
      "filter": {
        
        ... filter #1 
        
      },
      "aggs": {
        "metric": {
          "sum": {
            "script_file": "my_script",
            "params": {
              "factors": {
                "Male": 0.48,
                "Female": 1.8
              }
            }
          }
        }
      }
    },
    "agg2": {
      "filter": {
        
        ... filter #2 
        
      },
      "aggs": {
        "metric": {
          "sum": {
            "script_file": "my_script",
            "params": {
              "factors": {
                "Male": 0.48,
                "Female": 1.8
              }
            }
          }
        }
      }
    },
    ...
  }
}

```

About your suggestion - the weights are determined by the user, and not on index time (and it's very possible a later request will have different weights on the exact same data), so sadly no.

---

<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:** [January 3, 2017, 7:28pm UTC](https://discuss.elastic.co/t/multiple-sum-aggregations-using-the-same-script/67938/4 "2017-01-03T19:28:34Z")

</div>

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