# Aggregate across the document from Nested Documents

**URL:** <https://discuss.elastic.co/t/aggregate-across-the-document-from-nested-documents/221171>\
**Category:** Elasticsearch\
**Created:** [February 27, 2020, 8:25am UTC](https://discuss.elastic.co/t/aggregate-across-the-document-from-nested-documents/221171 "2020-02-27T08:25:30Z")\
**Posts on this page:** 2\
**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:** [February 27, 2020, 8:25am UTC](https://discuss.elastic.co/t/aggregate-across-the-document-from-nested-documents/221171/1 "2020-02-27T08:25:31Z")

</div>

Hello I have a document like this

Example Document 1

```auto
  "applicationId": "12345",
  "accountId": "test",
  "interactionInfo": [
    {
      "title": "body",
      "value": "test"
    },
    {
      "title": "UserLocation",
      "value": "US"
    },
  {
      "title": "CustomerLocation",
      "value": "IN"
    }
    {
      "title": "price",
      "value": "0.26"
    }
  ]
}

```

Example Document 2

```auto
{
  "applicationId": "12345",
  "accountId": "test",
  "interactionInfo": [
    {
      "title": "body",
      "value": "hello"
    },
    {
      "title": "UserLocation",
      "value": "GB"
    },
   {
      "title": "CustomerLocation",
      "value": "IN"
    }
    {
      "title": "price",
      "value": "0.45"
    }
  ]
}

```

Example Document 3

```auto
{
  "applicationId": "12345",
  "accountId": "test",
  "interactionInfo": [
    {
      "title": "body",
      "value": "hello"
    },
    {
      "title": "UserLocation",
      "value": "US"
    },
    {
      "title": "CustomerLocation",
      "value": "IN"
    }
    {
      "title": "price",
      "value": "0.30"
    }
  ]
}

```

where interaction info is the nested documents - I want to do an aggregation on the title **userLocation** and to calculate the total **price** based on the **userLocation**

Below is the aggregation query that i performed only gives me the list of countries presented in my document and i was stuck calculate the Sum of price for the particular user Location

```auto
"aggs": {
    "accountId": {
      "terms": {
        "field": "accountId",
        "size": 10
      },"aggs": {
        "Nested Aggregation": {
          "nested": {
            "path": "interactionInfo"
          },"aggs": {
            "Filter Based on Customer Location": {
              "filter": {
                "term": {
                  "interactionInfo.title.keyword": "UserLocation"
                }
              },"aggs": {
                "Country Wise": {
                  "terms": {
                    "field": "interactionInfo.value.keyword",
                    "size": 150
                  }
                }
              }
            }
          }
        }
      }
    }
  }

```

The above aggregation query will give me following result

```auto
        {
          "key": "test",
          "Nested Aggregation": {
            "Filter Based on Customer Location": {
              "Country Wise": {
                "buckets": [
                  {
                    "key": "US",
                    "doc_count": 2
                  },
                  {
                    "key": "GB",
                    "doc_count": 1
                  }
                ]
              }
            }
          }
        }

```

What I am Expecting is was to calculate Sum of all price based on the location. Is it possible to aggregate two different document in nested document at the same time.If it is possible means then how so.

```auto
{
  "Key" : "Test",
  "Bukets" : [
    {
      "Key" : "US",
       "doc_count" : "2",
       "Sum of Price" : {
          "sum" : "0.56"
       }
    },
    {
      "Key" : "GB",
      "doc_count" : "1",
      "Sum of Price" : {
          "sum" : "0.30"
       }
    }
    ]
}

```

Above is the result i am looking for.

---

<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:** [March 26, 2020, 8:25am UTC](https://discuss.elastic.co/t/aggregate-across-the-document-from-nested-documents/221171/2 "2020-03-26T08:25:31Z")

</div>

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