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!