# Sum aggregation on distinct values query

**URL:** <https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865>\
**Category:** Elasticsearch\
**Created:** [July 3, 2020, 11:19pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865 "2020-07-03T23:19:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giselle\_Castrejon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giselle_castrejon/32/71617_2.png) [@Giselle\_Castrejon](https://discuss.elastic.co/u/Giselle_Castrejon)\
**Post date:** [July 3, 2020, 11:19pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865/1 "2020-07-03T23:19:41Z")

</div>

Hi, I am having trouble with my sum aggregation. I am using Elasticsearch [7.6.1] and I cant get the sum of each field, my data is the following:

```
POST test_2/_doc/1
{
  "gender": [
        {
            "name": "other",
            "value": 12
        },
        {
            "name": "male",
            "value": 20
        },
        {
            "name": "male",
            "value": 20
        },
        {
            "name": "other",
            "value": 9
        },
        {
            "name": "female",
            "value": 10
        } 
    ]
}

```

And I have implemented the following:

```
GET test_2/_search
    {
      "aggs": {
        "gender_terms": {
           "terms": {
              "field": "gender.name.keyword",
             "size": 10
           },
           "aggs": {
              "gender_name_value_sums": {
                 "sum": {
                    "field": "gender.value"
                 }
              }
           }
        }
      }
    }

```

The result I am expectin to recieve is the sum of each gender:  
male: 40  
female: 10  
other: 22

But as a result I get the following:

```
"aggregations" : {
    "gender_terms" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "female",
          "doc_count" : 1,
          "gender_name_value_sums" : {
            "value" : 71.0
          }
        },
        {
          "key" : "male",
          "doc_count" : 1,
          "gender_name_value_sums" : {
            "value" : 71.0
          }
        },
        {
          "key" : "other",
          "doc_count" : 1,
          "gender_name_value_sums" : {
            "value" : 71.0
          }
        }
      ]
    }
  }

```

But why? I don't know why it is not showing me the correct vaules? 🤔

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 4, 2020, 6:06am UTC](https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865/2 "2020-07-04T06:06:19Z")

</div>

Welcome!

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

Coming back to your question you probably need to use `nested` type for the `gender` field as everything is by default flattened.

---

<div class="post-metadata">

**Author:** ![Giselle\_Castrejon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giselle_castrejon/32/71617_2.png) [@Giselle\_Castrejon](https://discuss.elastic.co/u/Giselle_Castrejon)\
**Post date:** [July 4, 2020, 8:50pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865/3 "2020-07-04T20:50:52Z")

</div>

Thanks @dadoonet ! I thought nested type was only implemented whe trying to obtain data from anothe API.

---

<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 1, 2020, 8:50pm UTC](https://discuss.elastic.co/t/sum-aggregation-on-distinct-values-query/239865/4 "2020-08-01T20:50:52Z")

</div>

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