# 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:** 1\
**Showing post:** 2

<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

---

_[View the full topic](https://discuss.elastic.co/t/iterate-over-hits-to-get-value-of-fields-for-my-hits-in-my-mail-box-body-from-watcher/48549)._
