# Getting compile error trying to access a field that include hyphens

**URL:** <https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [April 19, 2023, 12:32pm UTC](https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305 "2023-04-19T12:32:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rorii/32/133785_2.png) [@rorii](https://discuss.elastic.co/u/rorii)\
**Post date:** [April 19, 2023, 12:32pm UTC](https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305/1 "2023-04-19T12:32:30Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [April 19, 2023, 8:16pm UTC](https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305/2 "2023-04-19T20:16:42Z")

</div>

Hi rorii,

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

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [April 19, 2023, 10:27pm UTC](https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305/3 "2023-04-19T22:27:34Z")

</div>

Hi @rorii, for keys that have characters used in [operators](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-operators.html), 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']`.

---

<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:** [May 17, 2023, 10:27pm UTC](https://discuss.elastic.co/t/getting-compile-error-trying-to-access-a-field-that-include-hyphens/330305/4 "2023-05-17T22:27:39Z")

</div>

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