# Access parent bucket's key from child aggregation in geohash grid

**URL:** <https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310>\
**Category:** Elasticsearch\
**Created:** [May 1, 2014, 10:04pm UTC](https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310 "2014-05-01T22:04:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Gruner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_gruner/32/1599_2.png) [@Thomas\_Gruner](https://discuss.elastic.co/u/Thomas_Gruner)\
**Post date:** [May 1, 2014, 10:04pm UTC](https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310/1 "2014-05-01T22:04:22Z")

</div>

Hello!

I have been progressing well with aggregations, but this one has got me  
stumped.

I'm trying to figure out how to access the key of the parent bucket from a  
child aggregation.

The parent bucket is geohash\_grid, and the child aggregation is avg (trying  
to get avg lat and lon, but only for points that match the parent's  
bucket's geohash key)

Something like this:  
"aggregations" : {  
"LocationsGrid": {  
"geohash\_grid" : {  
"field" : "Locations",  
"precision" : 7,  
},  
"aggregations" : {  
"avg\_lat": {  
"avg": {  
"script": "if  
(doc['Locations'].value.geohash.startsWith(_parent\_bucket.key_))  
doc['Locations'].value.lat;"  
}  
}  
},  
}  
}

Thanks for any help or ideas with this!

--  
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/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 2, 2014, 5:59am UTC](https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310/2 "2014-05-02T05:59:59Z")

</div>

Hi,

Unfortunately, accessing the parent bucket key is not possible.

On Fri, May 2, 2014 at 12:04 AM, Thomas Gruner [tom.gruner@gmail.com](mailto:tom.gruner@gmail.com) wrote:

> Hello!
> 
> I have been progressing well with aggregations, but this one has got me  
> stumped.
> 
> I'm trying to figure out how to access the key of the parent bucket from a  
> child aggregation.
> 
> The parent bucket is geohash\_grid, and the child aggregation is avg  
> (trying to get avg lat and lon, but only for points that match the parent's  
> bucket's geohash key)
> 
> Something like this:  
> "aggregations" : {  
> "LocationsGrid": {  
> "geohash\_grid" : {  
> "field" : "Locations",  
> "precision" : 7,  
> },  
> "aggregations" : {  
> "avg\_lat": {  
> "avg": {  
> "script": "if  
> (doc['Locations'].value.geohash.startsWith(_parent\_bucket.key_))  
> doc['Locations'].value.lat;"  
> }  
> }  
> },  
> }  
> }
> 
> Thanks for any help or ideas with this!
> 
> --  
> 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/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j4fxXs4eQeh7SJ5KQyVbPuqd2bLKfWRmdRDsc%3DiCwkgdA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4fxXs4eQeh7SJ5KQyVbPuqd2bLKfWRmdRDsc%3DiCwkgdA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thomas\_Gruner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_gruner/32/1599_2.png) [@Thomas\_Gruner](https://discuss.elastic.co/u/Thomas_Gruner)\
**Post date:** [May 2, 2014, 4:10pm UTC](https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310/3 "2014-05-02T16:10:14Z")

</div>

Thank-you Adrien,

I managed to do a terms child aggregation on Locations and am averaging out  
the geohashes in the browser.

The js code is here:  
Query:  
[https://github.com/GlobAllomeTree/GlobAllomeTree/blob/elasticutils/globallometree/apps/allometric\_equations/static/allometric\_equations/js/map.js#L289](https://github.com/GlobAllomeTree/GlobAllomeTree/blob/elasticutils/globallometree/apps/allometric_equations/static/allometric_equations/js/map.js#L289)  
Parsing geohashes  
[https://github.com/GlobAllomeTree/GlobAllomeTree/blob/elasticutils/globallometree/apps/allometric\_equations/static/allometric\_equations/js/map.js#L352](https://github.com/GlobAllomeTree/GlobAllomeTree/blob/elasticutils/globallometree/apps/allometric_equations/static/allometric_equations/js/map.js#L352)

It seems to work very well and is still speedy.

Kind regards,

Tom

On Thursday, May 1, 2014 10:59:59 PM UTC-7, Adrien Grand wrote:

> Hi,
> 
> Unfortunately, accessing the parent bucket key is not possible.
> 
> On Fri, May 2, 2014 at 12:04 AM, Thomas Gruner \<[tom.g...@gmail.com](mailto:tom.g...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hello!
> > 
> > I have been progressing well with aggregations, but this one has got me  
> > stumped.
> > 
> > I'm trying to figure out how to access the key of the parent bucket from  
> > a child aggregation.
> > 
> > The parent bucket is geohash\_grid, and the child aggregation is avg  
> > (trying to get avg lat and lon, but only for points that match the parent's  
> > bucket's geohash key)
> > 
> > Something like this:  
> > "aggregations" : {  
> > "LocationsGrid": {  
> > "geohash\_grid" : {  
> > "field" : "Locations",  
> > "precision" : 7,  
> > },  
> > "aggregations" : {  
> > "avg\_lat": {  
> > "avg": {  
> > "script": "if  
> > (doc['Locations'].value.geohash.startsWith(_parent\_bucket.key_))  
> > doc['Locations'].value.lat;"  
> > }  
> > }  
> > },  
> > }  
> > }
> > 
> > Thanks for any help or ideas with this!
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/624d0bdd-c380-4c72-b642-e6afff3458a9%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
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/ba85de25-ca5e-41ba-988f-8449624525d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ba85de25-ca5e-41ba-988f-8449624525d1%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:32am UTC](https://discuss.elastic.co/t/access-parent-buckets-key-from-child-aggregation-in-geohash-grid/17310/4 "2017-07-06T01:32:03Z")

</div>


