# Accessing fields in watcher with transform actions

**URL:** <https://discuss.elastic.co/t/accessing-fields-in-watcher-with-transform-actions/230581>\
**Category:** Elasticsearch\
**Created:** [April 30, 2020, 3:14pm UTC](https://discuss.elastic.co/t/accessing-fields-in-watcher-with-transform-actions/230581 "2020-04-30T15:14:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sholzhauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sholzhauer/32/110282_2.png) [@sholzhauer](https://discuss.elastic.co/u/sholzhauer)\
**Post date:** [April 30, 2020, 3:14pm UTC](https://discuss.elastic.co/t/accessing-fields-in-watcher-with-transform-actions/230581/1 "2020-04-30T15:14:46Z")

</div>

Hi there,

I am trying to implement a watcher for when a client is sending "excessive" amounts of requests.  
When a client passes the threshold I would like to trigger an action foreach client which exceeds the threshold.

This is the first time for me using transform and painless and can't figure out the foreach and how to access the fields.

So my watcher:

```auto
{
	"trigger": {
		"schedule": {
			"interval": "5m"
		}
	},
	"input": {
		"search": {
			"request": {
				"body": {
					"query": {
						"bool": {
							"must": [{
									"range": {
										"@timestamp": {
											"gte": "now-5m"
										}
									}
								},
								{
									"match_phrase": {
										"tags": "haproxy"
									}
								}
							]
						}
					},
					"aggs": {
						"byClient": {
							"terms": {
								"field": "client"
							}
						}
					}
				},
				"indices": [
					"filebeat-*"
				]
			}
		}
	},
	"condition": {
		"array_compare": {
			"ctx.payload.aggregations.byClient.buckets": {
				"path": "doc_count",
				"gt": {
					"value": 600
				}
			}
		}
	},
	"transform": {
		"script": {
			"source": "List x = ctx.payload.aggregations.byClient.buckets.stream().filter(client -> client.doc_count >= params.threshold).collect(Collectors.toList()); return x;",
			"lang": "painless",
			"params": {
				"threshold": 600
			}
		}
	},
	"actions": {
    "foreach": "ctx.payload._value",
    "createSnowEvent": {
			"webhook": {
				"scheme": "https",
				"method": "POST",
				"host": "ENDPOINT",
				"port": 443,
				"path": "/api/",
				"body": "The client {{ctx.payload.key}} is sending {{ctx.payload.doc_count}} requests in the past 5 minutes",
				"auth": {
					"basic": {
						"username": "user",
						"password": "pwd"
					}
				},
				"headers": {
					"Content-Type": "application/json"
				}
			}
		}
	}
}

```

The current behaviour:

- The transform works, the action only has the 600+ clients.
- The action is triggered once, this is also the case when using `ctx.payload`
- I can't access `key` or `doc_count`

Thanks for reading

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 28, 2020, 3:27pm UTC](https://discuss.elastic.co/t/accessing-fields-in-watcher-with-transform-actions/230581/2 "2020-05-28T15:27:02Z")

</div>

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