# Is there a way to "chain" aggregations together?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-chain-aggregations-together/178524>\
**Category:** Elasticsearch\
**Created:** [April 25, 2019, 6:50pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-chain-aggregations-together/178524 "2019-04-25T18:50:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edave](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@edave](https://discuss.elastic.co/u/edave)\
**Post date:** [April 25, 2019, 6:50pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-chain-aggregations-together/178524/1 "2019-04-25T18:50:04Z")

</div>

Hi! I'm new to Elasticsearch, and have read a bunch of documentation but haven't been able to find if this is possible.

I'd like to compute an aggregation query (eg. mean), then use that value as input to a scripted metric aggregation.

I'd specifically like to:

Group by something then:  
1. Compute a mean, using some complicated weighting function  
2. Use the mean as an input to another aggregation to calculate another value, like standard deviation. I'd like to use the result of 1 for this group as an input for a scripted metric aggregation.

Right now I could do this by getting the return value of 1, then sending out a different query for each result of 1 to calculate 2, which seems inefficient, and clunky to code. So if I had 1000 groups, I'd send out one query to calculate 1, then 1000 to calculate 2. I'm wondering if there's a more elegant built-in way to do this. I've looked at bucket aggregations, but as far as I can tell those can only be used to combine multiple aggregations, I can't use one aggregation as input to another aggregation at the document level.

Is this possible? If so, can anybody point me to docs or an example? Thank you!

---

<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:** [May 23, 2019, 6:50pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-chain-aggregations-together/178524/2 "2019-05-23T18:50:18Z")

</div>

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