Getting compile error trying to access a field that include hyphens

I have a painless script that I use in my watcher and I need to access a nested element but for some reason I am getting a compile error.

I access the element in this way:
hits.hits[0]._source.getSomething.the-problematic-key-with-.errors.error-1

Everthing look fine until getSomething but if try to reach the-problematic-key-with- then I get a compile error in my watcher. I also tryied with Debug.explain() but it gives me the same result.

Here how the structure looks like.

"hits" : {
    "hits" : [
      {
        "_source" : {
          "getSomething" : {
            "the-problematic-key-with-" : {
              "errors" : {
                "error-1" : {
                  "level" : "WARNING",
                  "message" : "The message",
                },
                "error-2" : {...},
               
                }
              }
            }
          }
}
},

Hi rorii,

Are you able to share the full compile error you are seeing?

Hi @rorii, for keys that have characters used in operators, scripts need to use the brace map-syntax like so: hits.hits[0]['_source']['getSomething']['the-problematic-key-with-']['errors']['error-1'] .

Because the-problematic-key-with- and error-1 have the - character, which is the subtraction operator, the shortcut dot syntax for map keys does not work.

You can mix the two. hits.hits[0]._source.getSomething['the-problematic-key-with-'].errors['error-1'].

1 Like

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