Iterate over hits to get value of fields for my hits in my Mail box body from Watcher

Hey,

the number in the mustache template represents the index {{ctx.payload.hits.hits.0._source.CardNumber}}, so you could access other elements by increasing it. However what you really want, is looping through your results...

try this snippet and check your logs if that suits your use-case

DELETE foo

PUT foo/bar/1
{
  "name" : {
    "first" : "Kobe",
    "last" : "Bryant"
  }
}
PUT foo/bar/2
{
  "name" : {
    "first" : "Stephen",
    "last" : "Curry"
  }
}

PUT foo/bar/3
{
  "name" : {
    "first" : "Dirk",
    "last" : "Nowitzki"
  }
}

PUT /_watcher/watch/mustache-test
{
  "trigger" : {
    "schedule" : { "interval" : "1h" }
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/foo/_search"
      }
    }
  },
  "actions" : {
    "log" : {
      "logging" : {
        "text" : "{{#ctx.payload.hits.hits}} {{_source.name.first}} {{/ctx.payload.hits.hits}}"
      }
    }
  }
}

POST /_watcher/watch/mustache-test/_execute

And this should be the output

[2016-04-28 11:48:54,077][INFO ][watcher.actions.logging  ] [Weapon X]  Stephen Kobe Dirk

For more information, check out the search template documentation

If that is not powerful enough, you need to do a transform in your action.

Hope this helps

--Alex

1 Like