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

**URL:** <https://discuss.elastic.co/t/how-to-pass-context-variables-to-use-in-sub-aggregations-e-g--term/18675>\
**Category:** Elasticsearch\
**Created:** [July 15, 2014, 11:33am UTC](https://discuss.elastic.co/t/how-to-pass-context-variables-to-use-in-sub-aggregations-e-g--term/18675 "2014-07-15T11:33:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgeny\_Ryabitskiy](https://avatars.discourse-cdn.com/v4/letter/e/90ced4/32.png) [@Evgeny\_Ryabitskiy](https://discuss.elastic.co/u/Evgeny_Ryabitskiy)\
**Post date:** [July 15, 2014, 11:33am UTC](https://discuss.elastic.co/t/how-to-pass-context-variables-to-use-in-sub-aggregations-e-g--term/18675/1 "2014-07-15T11:33:15Z")

</div>

Hi,

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

Example:

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

"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  
script....

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eb618ee2-d71f-48d2-93cf-d18d4e3588eb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eb618ee2-d71f-48d2-93cf-d18d4e3588eb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:15am UTC](https://discuss.elastic.co/t/how-to-pass-context-variables-to-use-in-sub-aggregations-e-g--term/18675/2 "2017-07-06T01:15:44Z")

</div>

Closed on July 6, 2017, 1:15am UTC


