# Painless script to add value to array if its exists

**URL:** <https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580>\
**Category:** Logstash\
**Created:** [March 29, 2019, 4:56pm UTC](https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580 "2019-03-29T16:56:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas\_b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_b/32/43117_2.png) [@nicolas\_b](https://discuss.elastic.co/u/nicolas_b)\
**Post date:** [March 29, 2019, 4:56pm UTC](https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580/1 "2019-03-29T16:56:25Z")

</div>

Hello, i have an input with ( besides other data) 2 Arrays , event\_type and duration\_ms.

i use logstash to update the document with this data. Because i don't want to loose the data from event\_type and duration, i just add the values to an array in the original document.

My Problem is, that duration\_ms does not always exist, therefore i am currently trying to add an if statement, because otherwise the script would fail.

my problem is, that with the current config:

```
output {
  elasticsearch {
    index => "kafka_import"
    action => "update"
    doc_as_upsert => "true"
    document_id => "%{click_uuid}"
    script => 'ctx._source.event_type.add("%{event_type}"); if(ctx._source.duration_ms != null) { ctx._source.duration_ms.add("%{duration_ms}");}'
  }
}       

```

the output is

```
0, %{duration_ms}, %{duration_ms}, %{duration_ms}, %{duration_ms}, 9000, %{duration_ms}

```

Can someone point me into the right direction how to check if the field has a value?

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [March 29, 2019, 5:53pm UTC](https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580/2 "2019-03-29T17:53:15Z")

</div>

can you create a variable=value if it does not exist on filter level  
I have done this in one of the job. as some of the value was blank and it was getting rubyexception as I was trying to do some operation using ruby on that variable.

input {}  
filter {  
if ![variable] { mutate { update =\> { "variable" =\> "newvalue" } }  
}  
output {}

---

<div class="post-metadata">

**Author:** ![nicolas\_b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_b/32/43117_2.png) [@nicolas\_b](https://discuss.elastic.co/u/nicolas_b)\
**Post date:** [March 29, 2019, 6:43pm UTC](https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580/3 "2019-03-29T18:43:49Z")

</div>

So, i found a solution

```
        script => 'ctx._source.event_type.add("%{event_type}"); if (params.event.get("duration_ms") != null) {if(ctx._source.duration_ms != null) {ctx._source.duration_ms.add("%{duration_ms}");}}'

```

the `if (params.event.get("duration_ms") != null)` checks if the variable is defined in the values the i use to upsert the document and the `if(ctx._source.duration_ms != null)` checks if the document that gets updated has already a field like this

---

<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:** [April 26, 2019, 6:43pm UTC](https://discuss.elastic.co/t/painless-script-to-add-value-to-array-if-its-exists/174580/4 "2019-04-26T18:43:53Z")

</div>

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