Access aggregation key inside script

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 :

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

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

{
  "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 and the topic here. Seems it's not possible to access the aggregation data for sub-aggs. Do we have another workaround here? Thanks!!

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