# Watcher index action with dynamic doc\_id value

**URL:** <https://discuss.elastic.co/t/watcher-index-action-with-dynamic-doc-id-value/103787>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [October 12, 2017, 7:57pm UTC](https://discuss.elastic.co/t/watcher-index-action-with-dynamic-doc-id-value/103787 "2017-10-12T19:57:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acastellano](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@acastellano](https://discuss.elastic.co/u/acastellano)\
**Post date:** [October 12, 2017, 7:57pm UTC](https://discuss.elastic.co/t/watcher-index-action-with-dynamic-doc-id-value/103787/1 "2017-10-12T19:57:08Z")

</div>

I'm doing an index action in my watch's Actions block for docs from a \_doc payload (see code below). Index action works fine, however, I have duplicate docs and haven't figured out how to evaluate the "doc\_id" value in a dynamic way within my index action given a "\_doc" payload source:

```
"actions" : {
  "index_matches" : {
	"transform" : {
		"chain": [
		{ 
			"search" : {
				"request" : {
					"indices" : "rq-snow",
				    "body" : {
				      "size": 1000000,
						  "query" : { 
							  "match" : { 
								  "ip": "{{#ctx.payload.hits.hits}} {{_source.ip}} {{/ctx.payload.hits.hits}}"
							  }
						  }
					  }
			    }
		    }
		},	
		{
			"script": {
				"inline": "def document = []; for(item in ctx.payload.hits.hits) {document.add(item._source);} return [\"_doc\" : document];"
			}
		}
		]		
    },
	"index" : {
		"index" : "rq-snow-aware", 
		"doc_type" : "Hit",
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 13, 2017, 8:05am UTC](https://discuss.elastic.co/t/watcher-index-action-with-dynamic-doc-id-value/103787/2 "2017-10-13T08:05:02Z")

</div>

Hey,

you need a `_doc` array in the following structure to index by id.

```auto
_doc = [
 { "_id" : 1234, "foo":"bar", "spam": "eggs" }
]

```

This means you need to extract the metadata from the hit and add the id field as part of the second transform.

Hope this helps.

--Alex

---

<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:** [November 10, 2017, 8:05am UTC](https://discuss.elastic.co/t/watcher-index-action-with-dynamic-doc-id-value/103787/3 "2017-11-10T08:05:16Z")

</div>

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