# SumBucket on \_bucket\_count returns zero

**URL:** <https://discuss.elastic.co/t/sumbucket-on-bucket-count-returns-zero/122483>\
**Category:** Elasticsearch\
**Created:** [March 5, 2018, 10:38am UTC](https://discuss.elastic.co/t/sumbucket-on-bucket-count-returns-zero/122483 "2018-03-05T10:38:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shemmer](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@shemmer](https://discuss.elastic.co/u/shemmer)\
**Post date:** [March 5, 2018, 10:38am UTC](https://discuss.elastic.co/t/sumbucket-on-bucket-count-returns-zero/122483/1 "2018-03-05T10:38:11Z")

</div>

I want to count the number of buckets in an aggregation using ElasticSearch 5.6.2. My documents look like this:

```
"caseId": "123456",
"timestamp": "2018-02-26T12:28:30Z",
"status": "0"

```

The state of the last document entry determines the state of a case. My goal is to **compute the number of cases in a given state**. To that end, I have written the following query statement:

```
POST foo/foo/_search?size=0
{
  "aggs": {
    "caseID_termsAgg": {
      "terms": {
        "field": "caseId.keyword",
        "size": 5,
        "order": {
          "_term": "desc"
        }
      },
      "aggs": {
        "latest_status": {
          "max": {
            "field": "status"
          }
        },
        "topHits": {
          "top_hits": {
            "_source": "status",
            "size": 1,
            "sort": [
              {
                "timestamp": {
                  "order": "desc"
                }
              }
            ]
          }
        },
        "top_hits_filter": {
          "bucket_selector": {
            "buckets_path": {
              "processStatus": "latest_status"
            },
            "script": "params.processStatus == 1"
          }
        }
      }
    },
    "sum": {
      "sum_bucket": {
        "buckets_path": "caseID_termsAgg._bucket_count"
      }
    }
  }
}

```

I'm expecting the final aggregation of '_sum_' to count the number of buckets in caseID\_termsAgg. Instead what ES returns a 0 although there a multiple buckets visible in the query result. I tested replacing '\__bucket\_count_' with '_count_' and it correctly sums up the number of documents in each bucket. Can someone tell me what I'm doing wrong here? 🤔

---

<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:** [April 2, 2018, 10:38am UTC](https://discuss.elastic.co/t/sumbucket-on-bucket-count-returns-zero/122483/2 "2018-04-02T10:38:15Z")

</div>

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