Is there a way to escape hyphen (-) in painless

I'm trying to create a watcher to fetch the stats data for an index and writing it back to a new index
I have noticed when I'm using an index with name pcm the transform works but when I'm using a Index with name for example vmstat-pcm-test its failing ,
Is there way we can escape hyphen (-) in painless ?

Please find the Transform and Error message below.

Transform -
"transform": {
"script": {
"source": """

       def store_size_in_bytes = ctx.payload.indices.vmstat-pcm-test.primaries.store.size_in_bytes;
       
       def result_list = [];
       def data = ['@timestamp':ctx.trigger.triggered_time,'Store_Size' : store_size_in_bytes ];
       result_list.add ( data );
        
       return ['_doc': result_list ];
     """,
     "lang": "painless"
   }
 }

Error message -

"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... x.payload.indices.vmstat-pcm-test.primaries.store. ...",
" ^---- HERE"
],

caused_by": {
"type": "illegal_argument_exception",
"reason": "Variable [pcm] is not defined."
}

Hey,

try putting that name in brackets (['']) like this ctx.payload.indices['vmstat-pcm-test'].primaries.store.size_in_bytes.

You can also use brackets for every field if you want to, but in general I find the dot notation more readable.

--Alex

Thanks Alexander , it worked :smiley:

1 Like