Watcher - input - chain: conditional execution of a chain

I'm using the chain feature in a watcher.
I'm looking for a proper way to stop the execution of a specific input depending of the result of the previous one.

Let say, you have a 3 steps chain which do something like:

  • search query on an index
  • transform and adapt the result of the last result
  • execute a msearch with http input request with payload provided by last transform

Sample code below to illustrate.

If the result on first_search is empty, I don't want to execute the 3rd step of my inputs.
Is there anyway to do this ?
I know you can set a condition on an action, but I don't find a way to set a condition on input.

I was wondering if there was some solution to achieve this.

Thanks for your help.

{
  "trigger": {
    "schedule": {
      "interval": "10s"
    }
  },
  "input": {
    "chain": {
      "inputs": [
        {
          "first_search": {
            "search": {
              "request": {
                "search_type": "query_then_fetch",
                "indices": [
                  "my_index"
                ],
                "rest_total_hits_as_int": true,
                "body": {
                  "query": {
                    "bool": {
                      "filter": [
                        {"my wonderfull filter"},
                      ]
                    }
                  }
                  }
                }
              }
            }
          }
        },
        {
          "prepare_msearch_payload": {
            "transform": {
              "script": {
                "source": """
                      if (ctx.payload.first_search.hits != null) {
                                /* do some stuff to prepare data for next msearch */
                        } else { return null; }
                      """,
                "lang": "painless"
              }
            }
          }
        },
        {
          "last_msearch": {
            "http": {
              "request": {
                "scheme": "http",
                "host": "localhost",
                "port": 9200,
                "method": "post",
                "path": "my-wonderfull-index/_msearch",
                "params": {},
                "body": """{{#ctx.payload.prepare_msearch_payload._value}}{}
{{.}}
{{/ctx.payload.prepare_msearch_payload._value}}"""
              }
            }
          }
        }
      ]
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.last_msearch.hits.total": {
        "gte": 1
      }
    }
  },
  "actions": {
    "my-logging-action": {
      "logging": {
        "level": "info",
        "text": """ my log details"""
      }
    }
  }
}

Interrupting the chain is not supported. What you can do is to add a step in the chain of type transform and throw a RuntimeException. This will cause the next step in the chain to fail because of a null Payload.

"chain": {
  "inputs": [
       { "input_one": {} },
       {
          "skip_when": {
               "transform": {
                    "script": """
                         if (condition_to_skip) throw new RuntimeException('Crash the watcher');
                    """
                 }
           }
       },
       {"last_optional_input": {}}
   ]
}

The condition_to_skip it's up to you, something like -> ctx.payload.first_search.result_field.isEmpty()

1 Like

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