Alerting: Access @timestamp in transform throws Exception


(Nick Erber) #1

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?


Use @timestamp in a _update_by_query
(Alexander Reelsen) #2

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


(Nick Erber) #3

did exactly what i wanted, Thanks!


(system) #4

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