# Use doc field value in nested aggregation

**URL:** <https://discuss.elastic.co/t/use-doc-field-value-in-nested-aggregation/235745>\
**Category:** Elasticsearch\
**Created:** [June 4, 2020, 11:34am UTC](https://discuss.elastic.co/t/use-doc-field-value-in-nested-aggregation/235745 "2020-06-04T11:34:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)\
**Post date:** [June 4, 2020, 11:34am UTC](https://discuss.elastic.co/t/use-doc-field-value-in-nested-aggregation/235745/1 "2020-06-04T11:34:31Z")

</div>

My index has the following partly mapping:

```auto
"properties" : {
          "price" : {
            "type" : "double"
          },
          "services" : {
                   "type" : "nested",
                   "properties" : {
                       "quantity" : {
                            "type" : "double"
                        }
                   }
          },...

```

I am struggling to calculate sum of all `quantity` inside `services` and then multiple with `price` using painless script.  
When I use nested aggregation to the path `services`, the `doc['price']` is always 0.0.  
If I use reverse nested then I could not access the values of quantity.

Is there any way to achieve this using painless script?

---

<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 2, 2020, 11:34am UTC](https://discuss.elastic.co/t/use-doc-field-value-in-nested-aggregation/235745/2 "2020-07-02T11:34:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
