# Scripted metric aggregation over nested docs

**URL:** <https://discuss.elastic.co/t/scripted-metric-aggregation-over-nested-docs/23148>\
**Category:** Elasticsearch\
**Created:** [April 8, 2015, 12:51pm UTC](https://discuss.elastic.co/t/scripted-metric-aggregation-over-nested-docs/23148 "2015-04-08T12:51:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marko1](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@marko1](https://discuss.elastic.co/u/marko1)\
**Post date:** [April 8, 2015, 12:51pm UTC](https://discuss.elastic.co/t/scripted-metric-aggregation-over-nested-docs/23148/1 "2015-04-08T12:51:33Z")

</div>

I'm trying to do scripted metric aggregation  
[http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations-metrics-scripted-metric-aggregation.html](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations-metrics-scripted-metric-aggregation.html)  
using Groovy with Elasticsearch 1.4.4  
I've a document model where each parent document can have 0-n nested  
documents and I need to list distinct values and count their occurrences.

How can I access nested document field values in a script?  
With this map\_script fragment:  
for (r in doc['\_source.nested']) { \_agg.agg.add('abc') }

I get an error saying:  
"No field found for [\_source.nested] in mapping with types [foo]"

I've also tried omitting "\_source" from the field name and also doing a  
doc.containsKey check for the field but neither seem to work.

marko

--  
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/dc2ea339-f088-49b1-9dd9-1a428f28155b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dc2ea339-f088-49b1-9dd9-1a428f28155b%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, 12:20am UTC](https://discuss.elastic.co/t/scripted-metric-aggregation-over-nested-docs/23148/2 "2017-07-06T00:20:54Z")

</div>


