# Nested Query summation aggregation for a subset received in the source

**URL:** <https://discuss.elastic.co/t/nested-query-summation-aggregation-for-a-subset-received-in-the-source/272038>\
**Category:** Elasticsearch\
**Created:** [May 4, 2021, 6:57am UTC](https://discuss.elastic.co/t/nested-query-summation-aggregation-for-a-subset-received-in-the-source/272038 "2021-05-04T06:57:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RapTvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raptvin/32/86995_2.png) [@RapTvin](https://discuss.elastic.co/u/RapTvin)\
**Post date:** [May 4, 2021, 6:57am UTC](https://discuss.elastic.co/t/nested-query-summation-aggregation-for-a-subset-received-in-the-source/272038/1 "2021-05-04T06:57:55Z")

</div>

Hello everyone, I am trying for a an aggregated summation query on a nested object on the basis of a variable inside it and the summation on another variable present inside it. Till now I have been able to segregate the source on the basis of few terms.

Query

```auto
GET testing_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "testMeta.testCategory": "TEST"
          }
        }
      ]
    }
  },
  "_source": ["testingDetails.nestedDataItems.category","testingDetails.nestedDataItems.totalCount"]
}

```

Output:

```auto
"_source" : {
          "testingDetails" : {
            "nestedDataItems" : [
              {
                "category" : "ABC",
                "totalCount" : 447
              },
              {
                "category" : "XYZ",
                "totalCount" : 83
              }
            ]
          }
        }
      }

```

So the aggregation I need to implement is to add the `totalCount` for various documents according to the `category`

---

<div class="post-metadata">

**Author:** ![RapTvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raptvin/32/86995_2.png) [@RapTvin](https://discuss.elastic.co/u/RapTvin)\
**Post date:** [May 7, 2021, 7:16am UTC](https://discuss.elastic.co/t/nested-query-summation-aggregation-for-a-subset-received-in-the-source/272038/2 "2021-05-07T07:16:41Z")

</div>

Solved. If anyone faces similar issues then you may refer to this: [Bucket aggregations | Elasticsearch Guide [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html)

---

<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:** [June 4, 2021, 7:17am UTC](https://discuss.elastic.co/t/nested-query-summation-aggregation-for-a-subset-received-in-the-source/272038/3 "2021-06-04T07:17:26Z")

</div>

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