# Get bucket key within scripted\_metric

**URL:** <https://discuss.elastic.co/t/get-bucket-key-within-scripted-metric/167196>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 10:04pm UTC](https://discuss.elastic.co/t/get-bucket-key-within-scripted-metric/167196 "2019-02-05T22:04:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daddam](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@daddam](https://discuss.elastic.co/u/daddam)\
**Post date:** [February 5, 2019, 10:04pm UTC](https://discuss.elastic.co/t/get-bucket-key-within-scripted-metric/167196/1 "2019-02-05T22:04:12Z")

</div>

Hey guys, is there any way I can grab a bucket's key from within a scripted\_metric?  
I have an issue where I need to grab some specific data from within a document that is being aggregated.

For example, this is an example of the document I am working on:

```auto
{
    "attr1": "thing",
    "groups": [
        {
            "id": 1,
            "name": "foo"
        },
        {
            "id": 2,
            "name": "bar"
        },
        {
            "id": 3,
            "name": "baz"
        }
    ],
    "otherAttrs": true
}

```

**Figure 1** (Document structure)

I am doing a terms aggregation on the distinct group IDs, but within each bucket, I'd like to put the `name` of the group that is represented by the bucket\_key (which would be the `id`).

This is an example of the terms aggregation I am using:

```auto
{
    "terms": {
        "execution_hint": "global_ordinals_hash",
        "field": "actors.groups.id",
        "min_doc_count": 1
    }
}

```

**Figure 2** (Terms Aggregation to create buckets where I am trying to set `name` as a field)

So ideally my response would look something like this:

```auto
{
    "...": "...",
    "buckets" : [
        {
            "key" : 1,
            "group_name": "foo",
            "doc_count" : 42684,
            "measure 0" : {
                "value" : 37180
            },
            "measure 3" : {
                "doc_count" : 37180,
                "measure 3" : { "value" : 68 }
            },
            "measure 4" : {
                "doc_count" : 3008,
                "measure 4" : {
                    "value" : 3008
                }
            }
        }
    ]
}

```

**Figure 3** (Ideal Response format)

Notice how the `key` corresponds with the `name` found in **Figure 1**

So I am currently receiving a response similar to **Figure 3** (without `group_name`) and I cannot for the life of me figure out how to extract the `name` field because it's within a document being aggregated.

Due to the nature of the documents I'm working with, this has to happen within a bucket aggregation but this _one_ attribute is not an aggregation, it's just a single metric that I need to pluck off of one document.

So my attempt to solve this issue was to use a scripted\_metric:

```auto
{
    "...":"...",
    "group_name": {
        "scripted_metric": {
            "map_script": {
                "lang": "painless",
                "source": """

                for (HashMap group : params._source.actor.groups) {
                    String groupId = < bucket_key_here >;
                    if (groupId != null && !groupId.isEmpty()) {
                        params._aggs.name = params._source.actor.groups[groupId].name;
                    }
                }

                """
            },
            "reduce_script": {
                "lang": "painless",
                "source": "return params._aggs.length > 0 ? params._aggs[0].name : null;"
            }
        }
    },
    "...":"..."
}

```

**Figure 4** (Current attempt to use a scripted\_metric to tease out the group name)

I cannot figure out how to access the bucket's `key` value which means even if I use `_source` to access the JSON structure of the document being aggregated, I cannot see the _bucket_ in order to determine _which_ group is the correct name.

Notice in **Figure 1** that it's possible for _one_ document to contain multiple groups. So I need to be able to reference the `key` in order to match the `name` from the corresponding `id`.

Please let me know if I can clarify or expound on anything to make this issue more clear.

Regards,

Adam

---

<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:** [March 5, 2019, 10:04pm UTC](https://discuss.elastic.co/t/get-bucket-key-within-scripted-metric/167196/2 "2019-03-05T22:04:21Z")

</div>

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