Alerting: Access @timestamp in transform throws Exception

I am going to transform the @timestamp value of every hit in my watch and write it to another field.

When trying to access the @timestamp value with painless, I am receiving an error:

"type": "general_script_exception",
    "reason": "failed to compile script [ScriptException[compile error]; nested: IllegalArgumentException[unexpected character [@].]; nested: LexerNoViableAltException;]"
  },
  "status": 500

My transform-part looks as follows:

"transform" : {
    "script" : "for (int i=0; i<ctx.payload.hits.hits.size(); i++)
           { 
                ctx.payload.hits.hits[i].formatted_timestamp = ctx.payload.hits.hits[i]._source.@timestamp
           }
           return ctx.payload"           
        },

Is there any trick to access the @timestamp-field?

Hey,

try using ctx.payload.hits.hits[i]._source['@timestamp'], like in this example

POST _xpack/watcher/watch/_execute
{
  "watch" :{
    "trigger" : {
      "schedule" : { "interval" : "1h" }
    },
    "input" : {
      "simple" : {
        "foo" : {
          "@timestamp" : "my timestamp"
        }
      }
    },
    "transform" : {
      "script" : {
        "inline" : "ctx.payload.foo['@timestamp'] = 'bar' ; return ctx.payload"
      }
    },
    "actions" : {
      "logging" : {
        "logging" : {
          "text" : "{{ctx.payload}}"
        }
      }
    }
  }
}

--Alex

did exactly what i wanted, Thanks!

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