Finding Sum Aggregation for the properties from a Nested Document

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

  {
    "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"
      }
    ]
  }
]

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 and the nested query as well as the nested aggregation. Using nested stores the elements in an array in a different fashion allowing these kind of queries to work as expected.

--Alex

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

"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

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