# Getting the count of filtered buckets in elasticsearch

**URL:** https://discuss.elastic.co/t/getting-the-count-of-filtered-buckets-in-elasticsearch/209000
**Category:** Elasticsearch
**Created:** [November 22, 2019, 3:34am UTC](https://discuss.elastic.co/t/getting-the-count-of-filtered-buckets-in-elasticsearch/209000 "2019-11-22T03:34:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Rahul\_Jhawar1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahul_jhawar1/32/50716_2.png) [@Rahul\_Jhawar1](https://discuss.elastic.co/u/Rahul_Jhawar1)
#### Post date: [November 22, 2019, 3:34am UTC](https://discuss.elastic.co/t/getting-the-count-of-filtered-buckets-in-elasticsearch/209000/1 "2019-11-22T03:34:22Z")

</div>

Hi,I want to get the count of the buckets array after applying bucket\_filter in the aggregated bucket.This is how my document looks like-

```
{
          "cdid" : "CD1234",
          "transactionid" : "000000261119929",
          "itemid" : "612",
          "transactionDate" : "2019-09-17 00:00:09.196 +00:00",
          "quantity" : 2,
          "rate" : 7.12,
          "amount" : "7.12",
          "billAmt" : 248.48,
          "id" : "13"
 }

```

Since this is a transaction document I wanted to aggregate it with the `cdid` and filter all the documents whose `sum of quantity` is greater than 10.And then finally get the count of the total count of the customers satisfying the criteria.This is how I tried.

```
{
  "aggs": {
    "CDID": {
      "terms": {
        "field": "cdid.keyword"
      },
      "aggs": {
        "quanity": {
          "sum": {
            "field": "quantity",
            
          }
        },
        "bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "the_doc_count01": "quanity"
            },
            "script": "(params.the_doc_count01 > 10)"
          }
        }
      }
    },
    "count":{
      "cardinality": {
        "field": "CDID"
      }
    }
  }
}

```

But I'm getting the count of the first aggregated bucket and not the filtered bucket.Can anyone tell me how could I solve this

---

<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: [December 20, 2019, 3:34am UTC](https://discuss.elastic.co/t/getting-the-count-of-filtered-buckets-in-elasticsearch/209000/2 "2019-12-20T03:34:27Z")

</div>

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