Выполнение действия для каждого элемента в массиве
Вы можете использовать поле foreach в действии, чтобы запустить настроенное действие для каждого элемента в этом массиве.
Для защиты от длительных срабатываний смотрителей, вы можете использовать поле max_iterations, чтобы ограничить максимальное количество запусков, выполняемых каждым наблюдателем. Если этот предел достигнут, выполнение вежливо приостанавливается. Если это поле не задано, по умолчанию оно равно ста.
resp = client.watcher.put_watch(
id="log_event_watch",
trigger={
"schedule": {
"interval": "5m"
}
},
input={
"search": {
"request": {
"indices": "log-events",
"body": {
"query": {
"match": {
"status": "error"
}
}
}
}
}
},
condition={
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
actions={
"log_hits": {
"foreach": "ctx.payload.hits.hits",
"max_iterations": 500,
"logging": {
"text": "Found id {{ctx.payload._id}} with field {{ctx.payload._source.my_field}}"
}
}
},
)
print(resp) const response = await client.watcher.putWatch({
id: "log_event_watch",
trigger: {
schedule: {
interval: "5m",
},
},
input: {
search: {
request: {
indices: "log-events",
body: {
query: {
match: {
status: "error",
},
},
},
},
},
},
condition: {
compare: {
"ctx.payload.hits.total": {
gt: 0,
},
},
},
actions: {
log_hits: {
foreach: "ctx.payload.hits.hits",
max_iterations: 500,
logging: {
text: "Found id {{ctx.payload._id}} with field {{ctx.payload._source.my_field}}",
},
},
},
});
console.log(response); PUT _watcher/watch/log_event_watch
{
"trigger" : {
"schedule" : { "interval" : "5m" }
},
"input" : {
"search" : {
"request" : {
"indices" : "log-events",
"body" : {
"query" : { "match" : { "status" : "error" } }
}
}
}
},
"condition" : {
"compare" : { "ctx.payload.hits.total" : { "gt" : 0 } }
},
"actions" : {
"log_hits" : {
"foreach" : "ctx.payload.hits.hits",
"max_iterations" : 500,
"logging" : {
"text" : "Found id {{ctx.payload._id}} with field {{ctx.payload._source.my_field}}"
}
}
}
} | Заявление об операции протоколирования будет выполнено для каждого из возвращенных совпадений поиска. |
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/action-foreach.html