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

**URL:** <https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [April 27, 2016, 12:28pm UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549 "2016-04-27T12:28:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurav\_Harsola](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Gaurav\_Harsola](https://discuss.elastic.co/u/Gaurav_Harsola)\
**Post date:** [April 27, 2016, 12:28pm UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549/1 "2016-04-27T12:28:43Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 28, 2016, 9:50am UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549/2 "2016-04-28T09:50:31Z")

</div>

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

```auto
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

```auto
[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](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-template.html#_passing_an_array_of_strings)

If that is not powerful enough, you need to do a [transform](https://www.elastic.co/guide/en/watcher/current/transform.html) in your action.

Hope this helps

--Alex

---

<div class="post-metadata">

**Author:** ![Gaurav\_Harsola](https://avatars.discourse-cdn.com/v4/letter/g/ecc23a/32.png) [@Gaurav\_Harsola](https://discuss.elastic.co/u/Gaurav_Harsola)\
**Post date:** [April 28, 2016, 10:04am UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549/3 "2016-04-28T10:04:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xiaozhuqq48](https://avatars.discourse-cdn.com/v4/letter/x/97f17d/32.png) [@xiaozhuqq48](https://discuss.elastic.co/u/xiaozhuqq48)\
**Post date:** [October 23, 2016, 8:03am UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549/4 "2016-10-23T08:03:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:42pm UTC](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549/5 "2017-07-06T13:42:23Z")

</div>


