# ElasticSearch: access document nested value in groovy script

**URL:** <https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644>\
**Category:** Elasticsearch\
**Created:** [January 14, 2015, 6:55pm UTC](https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644 "2015-01-14T18:55:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Anil\_Kumar](https://discuss.elastic.co/u/Anil_Kumar)\
**Post date:** [January 14, 2015, 6:55pm UTC](https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644/1 "2015-01-14T18:55:15Z")

</div>

I have a document stored in ElasticSearch as below. \_source:

{  
"firstname": "John",  
"lastname": "Smith",  
"medals":[  
{  
"bucket": 100,  
"count": 1  
},  
{  
"bucket": 150,  
"count": 2  
}  
]  
}

I can access the string type value inside a document using doc.firstname for  
scripted metric aggregation  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)  
.

But I am not able to get the field value using doc.medals[0].bucket.

Can you please help me out and let me know how to access the values inside  
nested fields?

--  
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/60a4801a-3d76-4f1e-8156-f1cd30dec79b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/60a4801a-3d76-4f1e-8156-f1cd30dec79b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@Anil\_Kumar](https://discuss.elastic.co/u/Anil_Kumar)\
**Post date:** [January 15, 2015, 8:53pm UTC](https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644/2 "2015-01-15T20:53:03Z")

</div>

I found this.

I had to use \_source.medals to access the nested documents which are stored  
in disk and not in memory.

Thanks

On Wednesday, January 14, 2015 at 10:55:15 AM UTC-8, Anil Kumar wrote:

> I have a document stored in Elasticsearch as below. \_source:
> 
> {  
> "firstname": "John",  
> "lastname": "Smith",  
> "medals":[  
> {  
> "bucket": 100,  
> "count": 1  
> },  
> {  
> "bucket": 150,  
> "count": 2  
> }  
> ]  
> }
> 
> I can access the string type value inside a document using doc.firstname for  
> scripted metric aggregation  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)  
> .
> 
> But I am not able to get the field value using doc.medals[0].bucket.
> 
> Can you please help me out and let me know how to access the values inside  
> nested fields?

--  
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/19bd5fb9-b584-441f-8c55-c2f0d2b7d24e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19bd5fb9-b584-441f-8c55-c2f0d2b7d24e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jygoubet](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jygoubet](https://discuss.elastic.co/u/jygoubet)\
**Post date:** [February 12, 2016, 4:35pm UTC](https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644/3 "2016-02-12T16:35:24Z")

</div>

Hi,

I face the same issue but could not make it work with your solution, did you mean that using \_source.medals[0].bucket.value worked ?

I have a groovy script execution exception of type null pointer "Cannot invoke method getAt() on null object" :s

I know it was a long time ago but first round's on me if you know what's going on 😉

Thanks

---

<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 5, 2017, 11:16pm UTC](https://discuss.elastic.co/t/elasticsearch-access-document-nested-value-in-groovy-script/21644/4 "2017-07-05T23:16:48Z")

</div>


