# Scripted nested sum aggregation with top document field

**URL:** <https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326>\
**Category:** Elasticsearch\
**Created:** [July 10, 2015, 3:39pm UTC](https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326 "2015-07-10T15:39:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matteo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matteo/32/3657_2.png) [@Matteo](https://discuss.elastic.co/u/Matteo)\
**Post date:** [July 10, 2015, 3:39pm UTC](https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326/1 "2015-07-10T15:39:46Z")

</div>

Hi everybody,

I have a document that has two double fields one on the top level document and the other one into a nested object.I need to do a sum aggregation based on the script sum:{"script":"doc['nested.doubleValue'] \* doc['otherDoubleValue']"}.

I tried by doing a nested aggregation over the nested object and than back up with a reverse nested aggregation but it looks like I can't pass the value of the nested double property up to the reverse or viceversa.

Is there a standard way to do scripting using nested and root properties?

Thanks a lot  
Matteo

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 10, 2015, 4:41pm UTC](https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326/2 "2015-07-10T16:41:28Z")

</div>

I think to do this you will need to add `"include_in_root": true` to the mapping for the child document field so that it is available on the parent document. see [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html#\_mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html#_mapping) for more details

---

<div class="post-metadata">

**Author:** ![Matteo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matteo/32/3657_2.png) [@Matteo](https://discuss.elastic.co/u/Matteo)\
**Post date:** [July 12, 2015, 8:40am UTC](https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326/3 "2015-07-12T08:40:25Z")

</div>

That's exactly what I need: thanks a lot!

---

<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:02am UTC](https://discuss.elastic.co/t/scripted-nested-sum-aggregation-with-top-document-field/25326/4 "2017-07-06T00:02:07Z")

</div>


