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

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:

  "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:

      "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?

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

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

it fails and I get the following error:

      "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 :frowning:

sorry...missing braquet at the end :frowning: Issue solved.