# Access aggregation key inside script

**URL:** <https://discuss.elastic.co/t/access-aggregation-key-inside-script/314739>\
**Category:** Elasticsearch\
**Created:** [September 20, 2022, 6:20am UTC](https://discuss.elastic.co/t/access-aggregation-key-inside-script/314739 "2022-09-20T06:20:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WeiJun0827](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weijun0827/32/111033_2.png) [@WeiJun0827](https://discuss.elastic.co/u/WeiJun0827)\
**Post date:** [September 20, 2022, 6:20am UTC](https://discuss.elastic.co/t/access-aggregation-key-inside-script/314739/1 "2022-09-20T06:20:16Z")

</div>

Hello, I'm working on Elasticsearch v7.10. I stored the result of each game as a doc. The players and their scores were stored in `users` and `scores` arrays.

Sample data :

```auto
[
  {
    "gameId": "game01",
    "users": [
      "user01",
      "user02"
    ],
    "@timestamp": "2022-08-11T17:00:00.000Z",
    "scores": [
      4,
      1
    ]
  },
  {
    "gameId": "game02",
    "users": [
      "user01",
      "user02"
    ],
    "@timestamp": "2022-08-12T17:00:00.000Z",
    "scores": [
      3,
      1
    ]
  },
  {
    "gameId": "game02",
    "users": [
      "user02",
      "user03"
    ],
    "@timestamp": "2022-08-12T18:00:00.000Z",
    "scores": [
      2,
      4
    ]
  }
]

```

I expected to use the below query to aggregate the daily total scores of each game of users:

```auto
{
  "aggs": {
    "aggByDate": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "1d",
        "time_zone": "+8",
        "min_doc_count": 1
      },
      "aggs": {
        "aggByGame": {
          "terms": {
            "field": "gameId"
          },
          "aggs": {
            "aggByUser": {
              "terms": {
                "field": "users"
              },
              "aggs": {
                "totalScore": {
                  "sum": {
                    "script": {
                      "source": """
                        String targetUser = params.key; <--- I don't know how to get the key here
                        int i = 0;
                        for (def user: doc.users) {
                          if (user == targetUser) break;
                          i++;
                          }
                        return doc.scores[i];
                      """
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Expected result:

```auto
{
  "aggregations": {
    "aggByDate": {
      "buckets": [
        {
          "key_as_string": "2022-08-11T00:00:00.000+08:00",
          "doc_count": 1,
          "aggByGame": {
            "buckets": [
              {
                "key": "game01",
                "doc_count": 1,
                "aggByUser": {
                  "buckets": [
                    {
                      "key": "user01", <--- this is the value I want to inject into the script
                      "doc_count": 1,
                      "totalScore": {
                        "value": 4
                      }
                    },
                    {
                      "key": "user02",
                      "doc_count": 1,
                      "totalScore": {
                        "value": 1
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "key_as_string": "2022-08-12T00:00:00.000+08:00",
          "doc_count": 2,
          "aggByGame": {
            "buckets": [
              {
                "key": "game02",
                "doc_count": 1,
                "aggByUser": {
                  "buckets": [
                    {
                      "key": "user01",
                      "doc_count": 1,
                      "totalScore": {
                        "value": 3
                      }
                    },
                    {
                      "key": "user02",
                      "doc_count": 2,
                      "totalScore": {
                        "value": 3
                      }
                    },
                    {
                      "key": "user03",
                      "doc_count": 1,
                      "totalScore": {
                        "value": 4
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

But since I don't know the `userId` before the query, I don't know where can I let elasticsearch inject the `params.key` into the script in the middle of the query.

I also referenced [the issue here](https://github.com/elastic/elasticsearch/issues/56392#issuecomment-625865233) and [the topic here](https://discuss.elastic.co/t/access-aggregation-key-inside-script/91266). Seems it's not possible to access the aggregation data for sub-aggs. Do we have another workaround here? Thanks!!

---

<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:** [October 18, 2022, 6:21am UTC](https://discuss.elastic.co/t/access-aggregation-key-inside-script/314739/2 "2022-10-18T06:21:08Z")

</div>

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