Elastic search get bucket count

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.

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.

you can do like this:

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

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.

Are you have a test on my query?

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

the result like this:

"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
    }
  }