# Is there a way to get the key of the current aggregation bucket in script？

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-get-the-key-of-the-current-aggregation-bucket-in-script/288466>\
**Category:** Elasticsearch\
**Created:** [November 5, 2021, 5:27am UTC](https://discuss.elastic.co/t/is-there-a-way-to-get-the-key-of-the-current-aggregation-bucket-in-script/288466 "2021-11-05T05:27:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakey/32/103271_2.png) [@jakey](https://discuss.elastic.co/u/jakey)\
**Post date:** [November 5, 2021, 5:27am UTC](https://discuss.elastic.co/t/is-there-a-way-to-get-the-key-of-the-current-aggregation-bucket-in-script/288466/1 "2021-11-05T05:27:36Z")

</div>

For example: how to get the key (country) of the current bucket in the script after aggregating countries? Hope to give a solution, thank you！

```json
"aggs"{
	"country_group":{
		"terms":{
			"field":"country",
			"size":1000
		},"aggs":{
			"cty": {
          "scripted_metric": {
            "init_script": "",
            "map_script": "",
            "combine_script": "",
            "reduce_script": ""
          }
        }
		}
	}
}

```

---

<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 3, 2021, 5:28am UTC](https://discuss.elastic.co/t/is-there-a-way-to-get-the-key-of-the-current-aggregation-bucket-in-script/288466/2 "2021-12-03T05:28:22Z")

</div>

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