# Elastic search get bucket count

**URL:** <https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133>\
**Category:** Elasticsearch\
**Created:** [October 16, 2016, 4:43pm UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133 "2016-10-16T16:43:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mister\_L](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Mister\_L](https://discuss.elastic.co/u/Mister_L)\
**Post date:** [October 16, 2016, 4:43pm UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/1 "2016-10-16T16:43:57Z")

</div>

I have the following query:

GET images/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"term": {  
"appID.raw": "myApp"  
}  
}  
]  
}  
},  
"size": 0,  
"aggs": {  
"perDeviceAggregation": {  
"terms": {  
"field": "deviceID",  
"min\_doc\_count": 50000  
}  
}  
}

This query returns a "buckets" array, but I would like to change it so it returns only the length of the array, without the array itself.

Thanks.

---

<div class="post-metadata">

**Author:** ![Joshua\_Rich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshua_rich/32/44953_2.png) [@Joshua\_Rich](https://discuss.elastic.co/u/Joshua_Rich)\
**Post date:** [October 16, 2016, 11:37pm UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/2 "2016-10-16T23:37:46Z")

</div>

This sounds like something that could be done within your client? I'm not sure Elasticsearch provides an easy way to count the number of buckets.

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 17, 2016, 2:26am UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/3 "2016-10-17T02:26:34Z")

</div>

you can do like this:

```auto
POST index/_search
{
  "size": 0,
  "aggs": {
    "perDeviceAggregation": {
      "terms": {
        "field": "deviceID"
      }
    },
    "count":{
      "cardinality": {
        "field": "deviceID"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mister\_L](https://avatars.discourse-cdn.com/v4/letter/m/c68b51/32.png) [@Mister\_L](https://discuss.elastic.co/u/Mister_L)\
**Post date:** [October 17, 2016, 10:38am UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/4 "2016-10-17T10:38:28Z")

</div>

If I understand this correctly, your query will return the number of images per device, whereas what I want is to get the total number of devices that have a number of images above the threshold.

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 20, 2016, 7:36am UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/5 "2016-10-20T07:36:53Z")

</div>

> [@Mister\_L](#):
>
> threshold

Are you have a test on my query?

The query is return the total count deviceId and some deviceId details

---

<div class="post-metadata">

**Author:** ![wanglifengwf](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@wanglifengwf](https://discuss.elastic.co/u/wanglifengwf)\
**Post date:** [October 20, 2016, 7:42am UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/6 "2016-10-20T07:42:06Z")

</div>

the result like this:

```auto
"aggregations": {
    "aads": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "aa",
          "doc_count": 1
        },
       {
          "key": "bb",
          "doc_count": 1
        }
      ]
    },
    "count": {
      "value": 2
    }
  }

```

---

<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:** [July 5, 2017, 10:10pm UTC](https://discuss.elastic.co/t/elastic-search-get-bucket-count/63133/7 "2017-07-05T22:10:59Z")

</div>


