# Bucket key/item counting

**URL:** <https://discuss.elastic.co/t/bucket-key-item-counting/255826>\
**Category:** Kibana\
**Created:** [November 18, 2020, 11:09am UTC](https://discuss.elastic.co/t/bucket-key-item-counting/255826 "2020-11-18T11:09:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam7](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@sam7](https://discuss.elastic.co/u/sam7)\
**Post date:** [November 18, 2020, 11:09am UTC](https://discuss.elastic.co/t/bucket-key-item-counting/255826/1 "2020-11-18T11:09:15Z")

</div>

Hello,

I have a bucket that looks like this:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : ###,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "group" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "2222",
          "doc_count" : 64,
          "types_count" : {
            "value" : 64
          }
        },
        {
          "key" : "3333",
          "doc_count" : 6,
          "types_count" : {
            "value" : 6
          }
        },
        {
          "key" : "4444",
          "doc_count" : 5,
          "types_count" : {
            "value" : 5
          }
        },
        {
          "key" : "5555",
          "doc_count" : 15,
          "types_count" : {
            "value" : 15
          }
        }
      ]
    }
  }
}

```

I am using metric aggregation to get this bucket.

```auto
"aggs": {
                "group": {
                    "terms": {
                            "field": "record1.keyword"      
                            },
                    "aggs": {
                        "types_count" : { "value_count" : { "field" : "record1.keyword" } }
                        } 
                    }
                }

```

I want to count the number of keys in the bucket.

example - **Number of Record1 = 4**  
Is there a query or aggregation that does this?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [November 19, 2020, 6:54pm UTC](https://discuss.elastic.co/t/bucket-key-item-counting/255826/2 "2020-11-19T18:54:45Z")

</div>

It depends on what you are trying to do with this query. Is this part of a Kibana visualization? Is this something you're doing for an alert? One of the simpler ways to do this is using the `count` on the [stats bucket](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-stats-bucket-aggregation.html) pipeline agg, but it' s not support in Kibana UIs.

---

<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 17, 2020, 6:54pm UTC](https://discuss.elastic.co/t/bucket-key-item-counting/255826/3 "2020-12-17T18:54:51Z")

</div>

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