Find the value of a dynamic key inside an object

My partial mapping is as follows:

    "name": {
      "type": "string"
    },
    "misc": {
      "type": "object",
      "dynamic": "true",
      "properties": {}
    }

Let's assume there is only one document in my storage, as follows

    {
      "name": "foobar",
      "misc": {
        "201804": 1,
        "201805": 10,
        "201806": 2,
        "201807": 1,
        "201808": 9,
        "201809": 1,
        "201810": 8,
        "201811": 1,
        "201812": 11
      }
    }

In my search query, I have groovy script, where I am trying to find the value of the key inside misc object

    dynamic_key = ....my logical calculation to find key....
    //assume the calculated value of dynamic_key is "201808"
    kvalue = doc['misc'][dynamic_key].value //value of kvalue should be 9 as dynamic_key="201808"

But I keep getting error

type: "no_class_def_found_error",
reason: "java/lang/Throwable"

for this line kvalue = doc['misc'][dynamic_key].value . But for the experiment purpose, if I write kvalue = doc['misc.201805'].value then it works fine. Any idea how to achieve what I am looking for? I am using groovy script.

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