# Elasticsearch's output script running only for updated documents

**URL:** <https://discuss.elastic.co/t/elasticsearchs-output-script-running-only-for-updated-documents/165635>\
**Category:** Logstash\
**Created:** [January 24, 2019, 5:36pm UTC](https://discuss.elastic.co/t/elasticsearchs-output-script-running-only-for-updated-documents/165635 "2019-01-24T17:36:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ffknob](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ffknob/32/37008_2.png) [@ffknob](https://discuss.elastic.co/u/ffknob)\
**Post date:** [January 24, 2019, 5:36pm UTC](https://discuss.elastic.co/t/elasticsearchs-output-script-running-only-for-updated-documents/165635/1 "2019-01-24T17:36:37Z")

</div>

My pipeline receives two kind of unordered events: a parent event and possibly some children. Both will be grouped by a same id in one document in the index. The parent event will be written to the "parent" field and the children will be written to a "children" nested field.  
I can receive either one first. The first one that comes to the pipeline will generate the index document.  
For each child event I want to update a counter field in the index, so I thought I could do this with a script in the output. But it seens that this script is only running for events that happen to update the document.  
Could anyone explain the configuration below to me? I am a little confused by the correct combination of the action/doc\_as\_upsert/scripted\_upsert/upsert/script parameters...

Thank you

```
	elasticsearch {
    	hosts => ["vmsrv103:9200"]
    	index => "nfe"
    	document_type => "default"
    	document_id => "%{id}"
		action => "update"
  		doc_as_upsert => true
		script_lang => "painless"
		script_type => "inline"
		script => "
			ctx._source.put('test_field', 'only-to-check-how-many-events-pass-through-here');

		if(params.event.get('parent') == null) {
			if(ctx._source.qtyChildren == null) {
				ctx._source.put('qtyChildren', 1);
			} else {
				ctx._source.qtyChildren += 1;
			}
		} else {
			if(ctx._source.qtyChildren == null) {
				ctx._source.put('qtyChildren', 0);
			}
		}
		"
	}
```

---

<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:** [February 21, 2019, 5:36pm UTC](https://discuss.elastic.co/t/elasticsearchs-output-script-running-only-for-updated-documents/165635/2 "2019-02-21T17:36:46Z")

</div>

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