# How to get values from a bucket using the key(s) of that bucket instead of the index - elasticsearch bucket aggregation - watcher?

**URL:** <https://discuss.elastic.co/t/how-to-get-values-from-a-bucket-using-the-key-s-of-that-bucket-instead-of-the-index-elasticsearch-bucket-aggregation-watcher/309107>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [July 7, 2022, 11:54am UTC](https://discuss.elastic.co/t/how-to-get-values-from-a-bucket-using-the-key-s-of-that-bucket-instead-of-the-index-elasticsearch-bucket-aggregation-watcher/309107 "2022-07-07T11:54:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Souvik\_Das](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/souvik_das/32/107606_2.png) [@Souvik\_Das](https://discuss.elastic.co/u/Souvik_Das)\
**Post date:** [July 7, 2022, 11:54am UTC](https://discuss.elastic.co/t/how-to-get-values-from-a-bucket-using-the-key-s-of-that-bucket-instead-of-the-index-elasticsearch-bucket-aggregation-watcher/309107/1 "2022-07-07T11:54:19Z")

</div>

Hi team,

I have the below aggregation search query.

```auto
  "aggregations" : {
    "test_groups" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "group1",
          "doc_count" : 1,
          "emails" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "abc@xyz.com",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "group2",
          "doc_count" : 1,
          "emails" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "xyz@abc.com",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }

```

Now I have to extract the key value for the first bucket under `test_groups` i.e., "[abc@xyz.com](mailto:abc@xyz.com)". I successfully achieve this by using the following variable.  
`{{ctx.payload.first.aggregations.test_groups.buckets.0.emails.buckets.0.key}}`  
I am looking for an alternative of `test_groups.buckets.0.` and want something like `{{ctx.payload.first.aggregations.test_groups.buckets.group1.emails.buckets.0.key}}`.  
I want to use the key of the buckets instead of its index. Is it possible?

Please help!

Regards,  
Souvik

---

<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:** [August 4, 2022, 11:54am UTC](https://discuss.elastic.co/t/how-to-get-values-from-a-bucket-using-the-key-s-of-that-bucket-instead-of-the-index-elasticsearch-bucket-aggregation-watcher/309107/2 "2022-08-04T11:54:44Z")

</div>

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