How to pass context variables to use in sub aggregations e.g. _term


I wonder if there is any possibility to access upper level aggregation
results in sub aggregation?


Run a terms aggregation and get "_term" variable result in sub

"aggs" : {
"pairs" : {
"terms" : { "script" : "_source['pairs'].keySet()" }
aggs: {
"sum" : { "script" : "_source['pairs'].get(_term)" }

_term variable can be used for ordering of terms aggregation result but not
available at sub-aggregation.
It would be very nice if there is some way to provide it to sums
aggregation. I found in documentation that _value could be set outside

Best regards,
Evgeny Ryabitskiy.

You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit