# Append to a property if exists

**URL:** https://discuss.elastic.co/t/append-to-a-property-if-exists/214889
**Category:** Logstash
**Created:** [January 13, 2020, 6:29pm UTC](https://discuss.elastic.co/t/append-to-a-property-if-exists/214889 "2020-01-13T18:29:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sbezgoan](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@Sbezgoan](https://discuss.elastic.co/u/Sbezgoan)
#### Post date: [January 13, 2020, 6:29pm UTC](https://discuss.elastic.co/t/append-to-a-property-if-exists/214889/1 "2020-01-13T18:29:40Z")

</div>

Hello,

Input csv looks like :

```auto
 "","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 :

```auto
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 => ","
     columns => ["SNo","Code", "Date", "Time", "Open","High", "Low", "Close","Volume"]
     skip_header => true
  }
  mutate {
     add_field => {"DateTime" => "%{Date} %{Time}"}
}
 mutate {
     remove_field => ["%{Time}", "%{Date}"]
}
date{
  match => ["DateTime" , "YYYYMMdd HH:mm:ss"]
}
}
output {
   elasticsearch {
     hosts => "http://localhost:9200"
     index => "nse-stocks2"
     action => "update"
     doc_as_upsert => "true"
     document_id => "%{[SNo]}"
     manage_template => "true"
     script => 'if (ctx._source.Open == null) { ctx._source.Open = new ArrayList();} if(ctx._source.Open != null) {ctx._source.Open.add("%{[Open]}");}'
  }
stdout {}
}

```

The aim is if a document with the same ID already exists, then append to the Open field new incoming values creating an array of Open values.

The script doesn't seem to be doing the intended, appreciate any help on how I can achieve Open field mapping as a multi-valued array.

Also, I don't like the fact that I am allowing logstash to manage templates (elastic mappings).

I think the issue is the way I am retrieving the existing \_source, but not sure how to fix.

---

<div class="post-metadata">

### Author: ![Sbezgoan](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@Sbezgoan](https://discuss.elastic.co/u/Sbezgoan)
#### Post date: [January 13, 2020, 6:37pm UTC](https://discuss.elastic.co/t/append-to-a-property-if-exists/214889/2 "2020-01-13T18:37:56Z")

</div>

I have followed the discussion at

> [@Logstash Elasticsearch ouput: Append array while upserting](https://discuss.elastic.co/t/logstash-elasticsearch-ouput-append-array-while-upserting/70010/4):
>
> @magnusbaeck Thanks for your reply, I tried your suggestion: script =\> "ctx.\_source.shodan.protocols += %{[shodan][protocols]}" Results in the following error: "reason"=\>"compile error", "caused\_by"=\>{"type"=\>"illegal\_argument\_exception", "reason"=\>"invalid sequence of tokens near ['%'].", "caused\_by"=\>{"type"=\>"no\_viable\_alt\_exception" Same error with this: script =\> "%{[ctx][\_source][shodan][protocols]} += %{[shodan][protocols]}" It indicated some syntax error near the % symbol, I'm not…

---

<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 10, 2020, 6:37pm UTC](https://discuss.elastic.co/t/append-to-a-property-if-exists/214889/3 "2020-02-10T18:37:59Z")

</div>

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