# Append values to array field

**URL:** https://discuss.elastic.co/t/append-values-to-array-field/156476
**Category:** Logstash
**Created:** [November 13, 2018, 1:09pm UTC](https://discuss.elastic.co/t/append-values-to-array-field/156476 "2018-11-13T13:09:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![M.alsioufi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.alsioufi/32/52269_2.png) [@M.alsioufi](https://discuss.elastic.co/u/M.alsioufi)
#### Post date: [November 13, 2018, 1:09pm UTC](https://discuss.elastic.co/t/append-values-to-array-field/156476/1 "2018-11-13T13:09:36Z")

</div>

Hi,  
I am running Logstash to insert data from MySQL database to Elasticsearch.  
In MySQL database I have one-to-many relationship so one value from table A has zero or more values in table B.  
I have made the configuration and the suitable SQL statement and I am using jdbc logstash input plugin to do this.  
my config file looks like:

```
input {
	jdbc {
		jdbc_driver_library => "./mysql-connector-java-5.1.45-bin.jar"
		jdbc_driver_class => "com.mysql.jdbc.Driver"
		jdbc_connection_string => "jdbc:mysql://myhost:portnum/db_name"
		jdbc_user => "name"
		jdbc_password => "secret"
		schedule => "* * * * *"
		statement => "SELECT a, b
					  FROM A
					  JOIN B
					  ON A.b_id = B.id
					  GROUP BY a, b"
	}
}

output {
	elasticsearch {
		action => 'update'
		document_id => "%{a}"
		"hosts" => "http://myEShost:portnumber"
		"index" => "my_index"
	}
	stdout { 
		codec => json_lines 
	}
}

```

So I use field "a" as \_id in my index, however, for each "a" I have multiple "b" and I want all these values to be inserted as an array in field "b" in my index.

How can I do this?

---

<div class="post-metadata">

### Author: ![M.alsioufi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.alsioufi/32/52269_2.png) [@M.alsioufi](https://discuss.elastic.co/u/M.alsioufi)
#### Post date: [November 14, 2018, 9:20am UTC](https://discuss.elastic.co/t/append-values-to-array-field/156476/2 "2018-11-14T09:20:51Z")

</div>

After I read this topic:

> [@Logstash Elasticsearch ouput: Append array while upserting](https://discuss.elastic.co/t/logstash-elasticsearch-ouput-append-array-while-upserting/70010/5):
>
> 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" Are you 100% sure all events have a [shodan][protocols] field? You'll have to double-quote the variable expansion, i.e. do this: script =\> 'ctx.\_source.shodan.protocols += "%{[shodan][protocols]}"' Which version of Logstash are you using? script =\> "%{[ctx][\_source][shodan][protoc…

I modified the output config to become:  
` output {  
elasticsearch {  
"hosts" =\> "[http://myEShost](http://myEShost):portnumber"  
index =\> "my\_index"  
document\_id =\> "%{a}"  
action =\> "update"  
script =\> 'if(ctx.\_source.b != null) ctx.\_source.b.add("%{b}");'

```
        }
        stdout {
            codec => json_lines
        }
    }`

```

but this is not doing the job it gives "version conflict, current version [77] is different than the one provided [76]"

---

<div class="post-metadata">

### Author: ![M.alsioufi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m.alsioufi/32/52269_2.png) [@M.alsioufi](https://discuss.elastic.co/u/M.alsioufi)
#### Post date: [November 14, 2018, 2:45pm UTC](https://discuss.elastic.co/t/append-values-to-array-field/156476/3 "2018-11-14T14:45:36Z")

</div>

Thanks for your help -\_-

I managed to solve this by changing my output script field to  
`if (ctx._source.b == null) { ctx._source.b = new ArrayList(); } if(!ctx._source.b.contains("%{b}")) { ctx._source.b.add("%{b}"); }`

---

<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 12, 2018, 2:45pm UTC](https://discuss.elastic.co/t/append-values-to-array-field/156476/4 "2018-12-12T14:45:47Z")

</div>

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