# Sum of aggregated terms are not per bucket, but total

**URL:** https://discuss.elastic.co/t/sum-of-aggregated-terms-are-not-per-bucket-but-total/252541
**Category:** Elasticsearch
**Created:** [October 19, 2020, 12:44pm UTC](https://discuss.elastic.co/t/sum-of-aggregated-terms-are-not-per-bucket-but-total/252541 "2020-10-19T12:44:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![obayd.mir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/obayd.mir/32/77432_2.png) [@obayd.mir](https://discuss.elastic.co/u/obayd.mir)
#### Post date: [October 19, 2020, 12:44pm UTC](https://discuss.elastic.co/t/sum-of-aggregated-terms-are-not-per-bucket-but-total/252541/1 "2020-10-19T12:44:01Z")

</div>

I want to sum up a field value per bucket for that specific bucket, but instead of this, I'm getting the total for all buckets per bucket. See below for some details.

**Search request:**

```auto
GET index-001/_search
{
  "size": 0,
  "aggs": {
    "rate_per_need": {
      "terms": {
        "field": "Q4.label.keyword",
        "size": 25
      },
      "aggs": {
        "sum_rate": {
          "sum": {
            "field": "Q4.value"
          }
        }
      }
    }
  }
}

```

**Mapping**

```auto
{
  "index-001" : {
    "mappings" : {
      "properties" : {
        "Q4" : {
          "properties" : {
            "label" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "value" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}

```

**Response**

```auto
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "index-001",
        "_type" : "_doc",
        "_id" : "28",
        "_score" : 1.0,
        "_source" : {
          "Q4" : [
            {
              "label" : "Label A",
              "value" : 1
            },
            {
              "label" : "Label B",
              "value" : 2
            },
          ]
        }
      },
      {
        "_index" : "index-001",
        "_type" : "_doc",
        "_id" : "31",
        "_score" : 1.0,
        "_source" : {
          "Q4" : [
            {
              "label" : "Label A",
              "value" : 5
            },
            {
              "label" : "Label B",
              "value" : 3
            },
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "rate_per_need" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Label A",
          "doc_count" : 2,
          "sum_rate" : {
            "value" : 11.0 // expect 6 here
          }
        },
        {
          "key" : "Label B",
          "doc_count" : 2,
          "sum_rate" : {
            "value" : 11.0 // expect 5 here
          }
        }
      ]
    }
  }
}

```

For Key "Label A" I expect value 6 and for "Label B" I expect value 5, but instead, I get the sum of these two per bucket, which is 11. I'm not sure what the problem...

Can someone help me with this one?

---

<div class="post-metadata">

### Author: ![obayd.mir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/obayd.mir/32/77432_2.png) [@obayd.mir](https://discuss.elastic.co/u/obayd.mir)
#### Post date: [October 19, 2020, 9:14pm UTC](https://discuss.elastic.co/t/sum-of-aggregated-terms-are-not-per-bucket-but-total/252541/2 "2020-10-19T21:14:16Z")

</div>

Solved by changing the Q4 type to "nested" ([https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)). The reason why all buckets value were summed up was elasticsearch didn't know the elements in Q4 needed to be separated. More info on the link I set.

---

<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: [November 16, 2020, 9:14pm UTC](https://discuss.elastic.co/t/sum-of-aggregated-terms-are-not-per-bucket-but-total/252541/3 "2020-11-16T21:14:30Z")

</div>

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