Iterate over payload fields in Slack action

alerting

(Grzegorz Poznachowski) #1

Hi,
Can I iterate over ctx.payload.hits in Slack action text field for a specific field? I can use index to get specific one (with ctx.payload.hits.hits.0._id), but if there are more hits, is it possible to show all ids of the hits?


(Alexander Reelsen) #2

Hey,

you should check out the search template documentation

See this example

PUT /foo/bar/first-id
{ "foo":"bar"}

PUT /foo/bar/second-id
{ "foo":"bar"}

PUT _xpack/watcher/watch/my-watch
{
  "trigger": {
    "schedule": {
      "interval": "10s"
    }
  },
  "input": {
    "http": {
      "request": {
        "host": "localhost",
        "port": 9200,
        "path": "/foo/_search"
      }
    }
  },
  "actions": {
    "logging": {
      "logging": {
        "text": "{{#ctx.payload.hits.hits}}{{_id}}|{{/ctx.payload.hits.hits}}"
      }
    }
  }
}

results in

[2016-06-06 13:24:22,548][INFO ][xpack.watcher.actions.logging] [Cap 'N Hawk] first-id|second-id|

Hope this helps!

--Alex


(system) #3