# Logstash output script not working: Append array while upserting

**URL:** https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824
**Category:** Logstash
**Created:** [October 2, 2020, 4:53pm UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824 "2020-10-02T16:53:40Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bastian\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bastian_jager/32/57892_2.png) [@Bastian\_Jager](https://discuss.elastic.co/u/Bastian_Jager)
#### Post date: [October 2, 2020, 4:53pm UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/1 "2020-10-02T16:53:40Z")

</div>

My plan is to uploads logs with logstash and update a field if this is present already in ES.  
There is the field `"origin"` with maybe this `“origin” : [“live”]` and on upload it should become `“origin” : [“live, “upload]`. But I fail.

I saw some documentation like

> **[Elasticsearch output plugin | Logstash Reference \[8.11\] | Elastic](https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-script_type)**

and an old discussion

> [@Append to a property if exists](https://discuss.elastic.co/t/append-to-a-property-if-exists/214889):
>
> Hello, Input csv looks like : "","Code","Date","Time","Open","High","Low","Close","Volume" "1","3IINFOTECH",20150703,"09:16:00",5.55,4.55,4.55,4.55,835 "2","3IINFOTECH",20150703,"09:17:00",5.55,4.55,4.55,4.55,390 Logstash config is : input { file { path =\> "/Users/sbezgoan/Documents/Elastic/StockData/nse-company-stocks/three.csv" start\_position =\> "beginning" sincedb\_path =\> "/Users/sbezgoan/Documents/Tools/logstash-7.5.1/temp.log" } } filter { csv { separator =\> …

But I can not fix my issue.

I am using logstash 7.9.2. My config is working without the "script" part.

I am trying to append a field while/after uploading the document

```auto
    ....
    output {
	elasticsearch {
		hosts => "https://142.10.42.142:9200"
                ssl => true
                cacert => "/opt/some/elasticsearch-ca.pem"
		document_id => "%{doc_id}"
		index => "someName-c%{[customer][id]}-p%{[project][id]}-%{+YYYY-MM}"
		user => MasterOfNothing
		password => BestPasswortEver
        script => 'ctx._source.origin += "upload"'
        scripted_upsert => true
	}

```

But no field origin is created or appended

I also tried

```auto
    script => 'ctx._source.origin = "upload"'

```

or

```auto
    script => 'ctx._source.origin = ["upload"]'

```

to maybe just create that field, but also no effect.

What am I missing?

---

<div class="post-metadata">

### Author: ![Bastian\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bastian_jager/32/57892_2.png) [@Bastian\_Jager](https://discuss.elastic.co/u/Bastian_Jager)
#### Post date: [October 6, 2020, 7:12am UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/2 "2020-10-06T07:12:53Z")

</div>

Is there not even one person with better knowledge about this output script things? I need a solution for this case.

---

<div class="post-metadata">

### Author: ![Bastian\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bastian_jager/32/57892_2.png) [@Bastian\_Jager](https://discuss.elastic.co/u/Bastian_Jager)
#### Post date: [October 20, 2020, 3:57pm UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/3 "2020-10-20T15:57:55Z")

</div>

Anyone in the forum an idea? Any admin might read this?

---

<div class="post-metadata">

### Author: ![ToddDtown](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@ToddDtown](https://discuss.elastic.co/u/ToddDtown)
#### Post date: [October 20, 2020, 8:48pm UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/4 "2020-10-20T20:48:25Z")

</div>

Bastian... try this

script =\> "ctx.\_source.viewedPhotos.add(123)"

---

<div class="post-metadata">

### Author: ![Bastian\_Jager](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bastian_jager/32/57892_2.png) [@Bastian\_Jager](https://discuss.elastic.co/u/Bastian_Jager)
#### Post date: [November 11, 2020, 10:27am UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/5 "2020-11-11T10:27:55Z")

</div>

Thanks @ToddDtown for your reply, but not sure what else to put where.  
Didn't fix my issue.

In addition your idea might lead to multiple entries of the same value, I guess.

My solution now is to restructure and use a nested field with integers.

```
"origin": {
  "live": 1
  "upload": 1
},

```

so in the logstash config I just need this:

```
mutate {
    add_field => { "[origin][upload]" => 1 }
}
mutate {
    convert => ["[origin][upload]","integer"]
}

```

where the 2nd part is "just" to ensure having a integer instead of a string.

---

<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: [December 9, 2020, 10:27am UTC](https://discuss.elastic.co/t/logstash-output-script-not-working-append-array-while-upserting/250824/6 "2020-12-09T10:27:57Z")

</div>

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