# Watcher : parsing index to get values to be used in watchers

**URL:** <https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041>\
**Category:** Elasticsearch\
**Created:** [September 23, 2022, 5:10pm UTC](https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041 "2022-09-23T17:10:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnJ\_M](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JohnJ\_M](https://discuss.elastic.co/u/JohnJ_M)\
**Post date:** [September 23, 2022, 5:10pm UTC](https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041/1 "2022-09-23T17:10:54Z")

</div>

Hello the community, first post for me so please excuse my possible mistakes.

Here is my issue:

I am using the watcher in Elastic v7.13.4.

I am using watcher to send mail to my customers to produce audit report on different sources.

In my first version I was creating a specific watcher per customer. I evolved to one generic watcher that I copy past and change a value as per bellow:

```auto
  "input": {
    "chain": {
      "inputs": [
        {
          "BipBip": {
            "simple": {
              "name" : "TEST"
            }
          }
        }
     ]
  }
}

```

I then just have to use {{ctx.payload.BipBip.name}} in the rest of the watcher so each time I get a new customer to adress, I dont have to change the name value in all the watcher.

Now I am going the next step: I have created an index BipBip where I store all the diffrent information for this customer for the watcher, and I want to use the result to populate the values in the watcher. The final goal is having only 1 watcher that will create / delete watchers according an index.

Let's say for the example BipBip index has 2 fields:  
name  
email

I tried to do that:

```auto
      "inputs": [
        {
          "BipBip": {
            "simple": {
              // We define the Name value from the index we will look the value in
              "name" : "TEST"
            }
          }
        },
        {
          "BipBiplist" : {
            "search" : {
              "request": {
                "search_type": "query_then_fetch",
                "indices" : [
                  "BipBip"
                ],
                "rest_total_hits_as_int": true,
                "body": {
                  // not sure I need that
                  "size": 10,
                  // Query on the index
                  "query": {
                    "bool": {
                      "filter": [
                        {
                          "match_phrase": {
                            "name": "{{ctx.payload.BipBip.name}}"
                          }
                        }
                      ],
                    }
                  }
                }
              }
            }
          }

```

So far so good...but no, it does not work. I properly fetch the data from the index BipBip, but cant figure out to use the reuslt in the rest of the watcher.

I first tried to use {{ctx.payload.BipBiplist.name}} and {{ctx.payload.BipBiplist.email}} in the other filter I have in the chain input, but even if the watcher execution launch no errors, it does not use the values.

I thought then I should use a specific value (I have only one name per customer) so I used {{ctx.payload.BipBiplist.hits.0.fields.name}}...But it fails to.

Any help possible on that matter?

---

<div class="post-metadata">

**Author:** ![JohnJ\_M](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JohnJ\_M](https://discuss.elastic.co/u/JohnJ_M)\
**Post date:** [September 26, 2022, 2:50pm UTC](https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041/2 "2022-09-26T14:50:03Z")

</div>

Ok, I progress.

In fact, rather than using {{ctx.payload.BipBiplist.name}} I should use a start / end tag for the input I use:  
{{#ctx.payload.BipBiplist.hits.hits}}{{#\_source}}{{name}}{{/\_source}}{{/ctx.payload.BipBiplist.hits.hits}

=\> It works...but not everywhere.  
I am trying to send mail, so email adress are in one field of the Index Bipbip (email field).

When I used it like this

```auto
        "to": [
         "{{#ctx.payloadBipBiplist.hits.hits}}{{#_source}}{{email}}{{/_source}}{{/ctx.payload.BipBiplist.hits.hits}" 
        ],

```

it fails and I get the following error:

```auto
      "actions" : [
        {
          "id" : "email_1",
          "type" : "email",
          "status" : "failure",
          "error" : {
            "root_cause" : [
              {
                "type" : "script_exception",
                "reason" : "Mismatched start/end tags: ctx.payload.BipBiplist.hits.hits != ctx.payload.BipBiplist.hits.hits} in query-template:1 @[query-template:1]",
                "script_stack" : [],
                "script" : "{{#ctx.payload.BipBiplist.hits.hits}}{{#_source}}{{email}}{{/_source}}{{/ctx.payload.BipBiplist.hits.hits}",
                "lang" : "mustache"
              }
            ],
            "type" : "script_exception",
            "reason" : "Mismatched start/end tags: ctx.payload.BipBiplist.hits.hits != ctx.payload.BipBiplist.hits.hits} in query-template:1 @[query-template:1]",
            "script_stack" : [],
            "script" : "{{#ctx.payload.BipBiplist.hits.hits}}{{#_source}}{{email}}{{/_source}}{{/ctx.payload.BipBiplist.hits.hits}",
            "lang" : "mustache",
            "caused_by" : {
              "type" : "mustache_exception",
              "reason" : "Mismatched start/end tags: ctx.payload.BipBiplist.hits.hits != ctx.payload.BipBiplist.hits.hits} in query-template:1 @[query-template:1]"
            }
          }
        }
      ]
    },

```

which does not make sense to me ☹

---

<div class="post-metadata">

**Author:** ![JohnJ\_M](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@JohnJ\_M](https://discuss.elastic.co/u/JohnJ_M)\
**Post date:** [September 26, 2022, 3:49pm UTC](https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041/3 "2022-09-26T15:49:50Z")

</div>

sorry...missing braquet at the end ☹ Issue solved.

---

<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:** [October 24, 2022, 3:50pm UTC](https://discuss.elastic.co/t/watcher-parsing-index-to-get-values-to-be-used-in-watchers/315041/4 "2022-10-24T15:50:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
