# Finding Sum Aggregation for the properties from a Nested Document

**URL:** <https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611>\
**Category:** Elasticsearch\
**Created:** [July 3, 2019, 5:48am UTC](https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611 "2019-07-03T05:48:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thilak](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@Thilak](https://discuss.elastic.co/u/Thilak)\
**Post date:** [July 3, 2019, 5:48am UTC](https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611/1 "2019-07-03T05:48:58Z")

</div>

Hello  
I was trying to find the aggregate inside the nested document (interactionInfo) only for the title : segments

```auto
  {
    "interactionId": "a26617a0-0530-4b41-9db6-4ca53e83e63e",
    "interactionInfo": [
      {
        "title": "segments",
        "value": "1"
      },
      {
        "title": "email",
        "value": "testing"
      },
      {
        "title": "Order",
        "value": "2"
      }
    ]
  },
  {
    "interactionId": "a26617a0-0530-4b41-9db6-4ca53e83e63e",
    "interactionInfo": [
      {
        "title": "segments",
        "value": "4"
      },
      {
        "title": "email",
        "value": "testing"
      },
      {
        "title": "Order",
        "value": "3"
      }
    ]
  }
]
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 3, 2019, 8:30am UTC](https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611/2 "2019-07-03T08:30:41Z")

</div>

Hey,

please use markdown for formatting code snippets in here. It will make your example much more readable, and thus more likely to get an answer.

If you want to be able to execute queries or aggregations against elements within JSON arrays, you need to get familiar with the [nested datatype](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/nested.html) and the [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/query-dsl-nested-query.html) as well as the [nested aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-aggregations-bucket-nested-aggregation.html). Using nested stores the elements in an array in a different fashion allowing these kind of queries to work as expected.

--Alex

---

<div class="post-metadata">

**Author:** ![Thilak](https://avatars.discourse-cdn.com/v4/letter/t/4af34b/32.png) [@Thilak](https://discuss.elastic.co/u/Thilak)\
**Post date:** [July 8, 2019, 6:15am UTC](https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611/3 "2019-07-08T06:15:02Z")

</div>

Hey Alexander Reelsen  
This is the sum aggregation i have to using to calculate the the total value. But I am getting the aggregate for both the title ( Segments and Order) . is there a way to get aggregate for the title ( Segments ) alone

```auto
"aggs": {
    "Sum": {
      "nested": {
        "path": "interactionInfo"
      },
      "aggs": {
        "Sum": {
          "sum": {
            "script": "Integer.parseInt(doc['interactionInfo.value.keyword'].value)"
          }
        }
      }
    }
  }

Along with the response i am getting reason in the JSON 

  "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [            
 "java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)",
            "java.lang.Integer.parseInt(Integer.java:592)",
            "java.lang.Integer.parseInt(Integer.java:615)",
            "Integer.parseInt(doc['interactionInfo.value.keyword'].value)",
            " ^---- HERE"
          ],
          "script": "Integer.parseInt(doc['interactionInfo.value.keyword'].value)",
          "lang": "painless",
          "caused_by": {
            "type": "number_format_exception",
            "reason": """For input string: """""
          }
        }
is there any other way to avoid the reason in the response
```

---

<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:** [August 5, 2019, 6:15am UTC](https://discuss.elastic.co/t/finding-sum-aggregation-for-the-properties-from-a-nested-document/188611/4 "2019-08-05T06:15:17Z")

</div>

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