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

Hi,

Let i got 4 hits when i run my filter query in my input section of watcher.

And Under action ,i want to send the value of all hits field in the body of my mail box.So How i iterate for loop over my hits in action section of my watcher.

"actions": {
    "email_admin": {
        "email": {
            "to": "'xyz'",
            "subject": "{{ctx.watch_id}} executed",
            "body": "{{ctx.watch_id}} the full data  {{ctx.payload}} extarct field {{ctx.payload.hits.hits.0._source.CardNumber}} hits"
        }
    }
}

}

here,I am getting only 0 index cardNumber and for rest of other hit, how should i get ?

Thanks
Gaurav

1 Like

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

Hi

i have following hits
hits = {
hits = [{
_index = fraud,
_type = logs,
_source = {
CardNumber = 909925232349,
msg = Disputed Transaction
},
_id = AVRM964B2x5WPwIZ6Ls5,
_score = 6.298317
}, {
_index = fraud,
_type = logs,
_source = {
CardNumber = 376353555565,
msg = Disputed Transaction,
},
_id = AVRM960f2x5WPwIZ6Lo9,
_score = 6.298317
}, {
_index = fraud,
_type = logs,
_source = {
CardNumber = 376353555565,
msg = Disputed Transaction}

I am getting this data from {[ ctx.payload }} .Now i want to send all teh detail of each hit on my mail box using command {{ctx.payload.hits.hits.0._source.CardNumber}} it only give cardnumber of first hit .Question is how should i get the cardnumber for the rest of hits in the same mail .

Thanks
Gaurav

1 Like

Have you solved this problem?
I also want to print the explict messages of all the input lines iteratorly in one email body.
Thanks.