|
| 1 | +summary: Run a watch inline |
| 2 | +method_request: POST _watcher/watch/_execute |
| 3 | +description: > |
| 4 | + Run `POST _watcher/watch/_execute` to run a watch inline. All other settings for this API still apply when inlining a watch. In |
| 5 | + this example, while the inline watch defines a compare condition, during the execution this condition will be ignored. |
| 6 | +# type: request |
| 7 | +value: |- |
| 8 | + { |
| 9 | + "watch" : { |
| 10 | + "trigger" : { "schedule" : { "interval" : "10s" } }, |
| 11 | + "input" : { |
| 12 | + "search" : { |
| 13 | + "request" : { |
| 14 | + "indices" : [ "logs" ], |
| 15 | + "body" : { |
| 16 | + "query" : { |
| 17 | + "match" : { "message": "error" } |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + }, |
| 23 | + "condition" : { |
| 24 | + "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }} |
| 25 | + }, |
| 26 | + "actions" : { |
| 27 | + "log_error" : { |
| 28 | + "logging" : { |
| 29 | + "text" : "Found {{ctx.payload.hits.total}} errors in the logs" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +alternatives: |
| 36 | + - language: Python |
| 37 | + code: |- |
| 38 | + resp = client.watcher.execute_watch( |
| 39 | + watch={ |
| 40 | + "trigger": { |
| 41 | + "schedule": { |
| 42 | + "interval": "10s" |
| 43 | + } |
| 44 | + }, |
| 45 | + "input": { |
| 46 | + "search": { |
| 47 | + "request": { |
| 48 | + "indices": [ |
| 49 | + "logs" |
| 50 | + ], |
| 51 | + "body": { |
| 52 | + "query": { |
| 53 | + "match": { |
| 54 | + "message": "error" |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + "condition": { |
| 62 | + "compare": { |
| 63 | + "ctx.payload.hits.total": { |
| 64 | + "gt": 0 |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + "actions": { |
| 69 | + "log_error": { |
| 70 | + "logging": { |
| 71 | + "text": "Found {{ctx.payload.hits.total}} errors in the logs" |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }, |
| 76 | + ) |
| 77 | + - language: JavaScript |
| 78 | + code: |- |
| 79 | + const response = await client.watcher.executeWatch({ |
| 80 | + watch: { |
| 81 | + trigger: { |
| 82 | + schedule: { |
| 83 | + interval: "10s", |
| 84 | + }, |
| 85 | + }, |
| 86 | + input: { |
| 87 | + search: { |
| 88 | + request: { |
| 89 | + indices: ["logs"], |
| 90 | + body: { |
| 91 | + query: { |
| 92 | + match: { |
| 93 | + message: "error", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + condition: { |
| 101 | + compare: { |
| 102 | + "ctx.payload.hits.total": { |
| 103 | + gt: 0, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + actions: { |
| 108 | + log_error: { |
| 109 | + logging: { |
| 110 | + text: "Found {{ctx.payload.hits.total}} errors in the logs", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | + - language: Ruby |
| 117 | + code: |- |
| 118 | + response = client.watcher.execute_watch( |
| 119 | + body: { |
| 120 | + "watch": { |
| 121 | + "trigger": { |
| 122 | + "schedule": { |
| 123 | + "interval": "10s" |
| 124 | + } |
| 125 | + }, |
| 126 | + "input": { |
| 127 | + "search": { |
| 128 | + "request": { |
| 129 | + "indices": [ |
| 130 | + "logs" |
| 131 | + ], |
| 132 | + "body": { |
| 133 | + "query": { |
| 134 | + "match": { |
| 135 | + "message": "error" |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
| 142 | + "condition": { |
| 143 | + "compare": { |
| 144 | + "ctx.payload.hits.total": { |
| 145 | + "gt": 0 |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + "actions": { |
| 150 | + "log_error": { |
| 151 | + "logging": { |
| 152 | + "text": "Found {{ctx.payload.hits.total}} errors in the logs" |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + ) |
| 159 | + - language: PHP |
| 160 | + code: |- |
| 161 | + $resp = $client->watcher()->executeWatch([ |
| 162 | + "body" => [ |
| 163 | + "watch" => [ |
| 164 | + "trigger" => [ |
| 165 | + "schedule" => [ |
| 166 | + "interval" => "10s", |
| 167 | + ], |
| 168 | + ], |
| 169 | + "input" => [ |
| 170 | + "search" => [ |
| 171 | + "request" => [ |
| 172 | + "indices" => array( |
| 173 | + "logs", |
| 174 | + ), |
| 175 | + "body" => [ |
| 176 | + "query" => [ |
| 177 | + "match" => [ |
| 178 | + "message" => "error", |
| 179 | + ], |
| 180 | + ], |
| 181 | + ], |
| 182 | + ], |
| 183 | + ], |
| 184 | + ], |
| 185 | + "condition" => [ |
| 186 | + "compare" => [ |
| 187 | + "ctx.payload.hits.total" => [ |
| 188 | + "gt" => 0, |
| 189 | + ], |
| 190 | + ], |
| 191 | + ], |
| 192 | + "actions" => [ |
| 193 | + "log_error" => [ |
| 194 | + "logging" => [ |
| 195 | + "text" => "Found {{ctx.payload.hits.total}} errors in the logs", |
| 196 | + ], |
| 197 | + ], |
| 198 | + ], |
| 199 | + ], |
| 200 | + ], |
| 201 | + ]); |
| 202 | + - language: curl |
| 203 | + code: |
| 204 | + "curl -X POST -H \"Authorization: ApiKey $ELASTIC_API_KEY\" -H \"Content-Type: application/json\" -d |
| 205 | + '{\"watch\":{\"trigger\":{\"schedule\":{\"interval\":\"10s\"}},\"input\":{\"search\":{\"request\":{\"indices\":[\"logs\"],\"b\ |
| 206 | + ody\":{\"query\":{\"match\":{\"message\":\"error\"}}}}}},\"condition\":{\"compare\":{\"ctx.payload.hits.total\":{\"gt\":0}}},\ |
| 207 | + \"actions\":{\"log_error\":{\"logging\":{\"text\":\"Found {{ctx.payload.hits.total}} errors in the logs\"}}}}}' |
| 208 | + \"$ELASTICSEARCH_URL/_watcher/watch/_execute\"" |
| 209 | + - language: Java |
| 210 | + code: | |
| 211 | + client.watcher().executeWatch(e -> e |
| 212 | + .watch(w -> w |
| 213 | + .actions("log_error", a -> a |
| 214 | + .logging(l -> l |
| 215 | + .text("Found {{ctx.payload.hits.total}} errors in the logs") |
| 216 | + ) |
| 217 | + ) |
| 218 | + .condition(c -> c |
| 219 | + .compare(NamedValue.of("ctx.payload.hits.total",Pair.of(ConditionOp.Gt,FieldValue.of(0)))) |
| 220 | + ) |
| 221 | + .input(i -> i |
| 222 | + .search(s -> s |
| 223 | + .request(r -> r |
| 224 | + .body(b -> b |
| 225 | + .query(q -> q |
| 226 | + .match(m -> m |
| 227 | + .field("message") |
| 228 | + .query(FieldValue.of("error")) |
| 229 | + ) |
| 230 | + ) |
| 231 | + ) |
| 232 | + .indices("logs") |
| 233 | + ) |
| 234 | + ) |
| 235 | + ) |
| 236 | + .trigger(t -> t |
| 237 | + .schedule(sc -> sc |
| 238 | + .interval(in -> in |
| 239 | + .time("10s") |
| 240 | + ) |
| 241 | + ) |
| 242 | + ) |
| 243 | + ) |
| 244 | + ); |
0 commit comments