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