# Count buckets of aggregation

**URL:** <https://discuss.elastic.co/t/count-buckets-of-aggregation/167679>\
**Category:** Elasticsearch\
**Created:** [February 8, 2019, 7:05pm UTC](https://discuss.elastic.co/t/count-buckets-of-aggregation/167679 "2019-02-08T19:05:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio\_Freitas](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@Fabio\_Freitas](https://discuss.elastic.co/u/Fabio_Freitas)\
**Post date:** [February 8, 2019, 7:05pm UTC](https://discuss.elastic.co/t/count-buckets-of-aggregation/167679/1 "2019-02-08T19:05:21Z")

</div>

Hi !

I'am getting it as response after bucket\_selector:

```
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 10,
        "successful": 10,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "groupby_id": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "SQ1dioA8pOktHQdyz",
                    "doc_count": 1,
                    "is_confirmation_filter": {
                        "doc_count": 1
                    }
                },
                {
                    "key": "uuyjhiHQdyz1",
                    "doc_count": 1,
                    "is_confirmation_filter": {
                        "doc_count": 1
                    }
                }
            ]
        }
    }

```

So, how can i get the count of "buckets" result of aggregation and bucket\_selector ?  
In this case, count equals 2.  
Sorry about anything... starting to learn about elastic use today.

This is my query:

/elasticsearch/confirmations,blocks/\_search?

```
{
   "size":0,
   "query":{
      "bool":{
         "should":[
            {
               "bool":{
                  "must":[
                     {
                        "term":{
                           "pK":"hRFFE9g0wThY1Z"
                        }
                     },
                     {
                        "term":{
                           "date":1549677600000
                        }
                     }
                  ]
               }
            },
            {
               "bool":{
                  "must":[
                     {
                        "term":{
                           "mK":"SQ1dioA8pOktHQdyz"
                        }
                     },
                     {
                        "bool":{
                           "must_not":{
                              "exists":{
                                 "field":"pK"
                              }
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   },
   "aggs":{
      "groupby_id":{
         "terms":{
            "field":"u",
            "size":25,
            "order":{
               "_count":"asc"
            }
         },
         "aggs":{
            "is_confirmation_filter":{
               "filter":{
                  "bool":{
                     "filter":[
                        {
                           "exists":{
                              "field":"pK"
                           }
                        }
                     ]
                  }
               }
            },
            "average_distinct":{
               "bucket_selector":{
                  "buckets_path":{
                     "group_by_count":"_count",
                     "is_confirmation":"is_confirmation_filter._count"
                  },
                  "script":"params.is_confirmation > 0 && params.group_by_count <2"
               }
            }
         }
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 15, 2019, 7:41pm UTC](https://discuss.elastic.co/t/count-buckets-of-aggregation/167679/2 "2019-02-15T19:41:37Z")

</div>

There's also a `_bucket_count` property which you can use: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#\_special\_paths](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#_special_paths)

---

<div class="post-metadata">

**Author:** ![Fabio\_Freitas](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@Fabio\_Freitas](https://discuss.elastic.co/u/Fabio_Freitas)\
**Post date:** [March 6, 2019, 2:55pm UTC](https://discuss.elastic.co/t/count-buckets-of-aggregation/167679/3 "2019-03-06T14:55:48Z")

</div>

Thx!

---

<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 3, 2019, 2:55pm UTC](https://discuss.elastic.co/t/count-buckets-of-aggregation/167679/4 "2019-04-03T14:55:51Z")

</div>

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