Get bucket key within scripted_metric

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:

{
    "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:

{
    "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:

{
    "...": "...",
    "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:

{
    "...":"...",
    "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

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