Sum of aggregated terms are not per bucket, but total

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:

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

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

Response

{
  "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?

Solved by changing the Q4 type to "nested" (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.

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