# Append to a array field

**URL:** <https://discuss.elastic.co/t/append-to-a-array-field/82649>\
**Category:** Elasticsearch\
**Created:** [April 18, 2017, 2:38am UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649 "2017-04-18T02:38:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 2:38am UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/1 "2017-04-18T02:38:26Z")

</div>

Hello, I am new to ES and but I'm getting the hang of it.  
It's a really powerful piece of software, but I have to say that the documentation is really lacking and confusing some times.

Here's my question:  
I have an integer array, that looks like this:

> `"hits_history" : [0,0]`

I want to append an integer to that array via an "update\_by\_query" call, I searched and found this link: [Update API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html)  
which has this example:

```
POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "blue"
        }
    }
}

```

so I tried:

> ```
> curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
> {
> "script": {
> "inline": "ctx._source.hits_history.add(params.hits)",
> "params": {"hits": 0}
> },
> "query": {
> "match_all": {}
> }
> }
> '
> 
> ```

but it gave me this error:

> ```
> "ctx._source.hits_history.add(params.hits); ",
> " ^---- HERE"
> "type" : "script_exception",
> "reason" : "runtime error",
> "caused_by" : {
> "type" : "illegal_argument_exception",
> "reason" : "Unable to find dynamic method [add] with [1] arguments for class [java.lang.Integer]."
> 
> ```

So, I looked further and found this: [Partial Updates to Documents | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html)

which has this example:

> We can also use a script to add a new tag to the tags array.

> ```
> POST /website/blog/1/_update
> {
> "script" : "ctx._source.tags+=new_tag",
> "params" : {
> "new_tag" : "search"
> }
> }
> 
> ```

So I tried it:

> ```
> curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
> {
> "script": {
> "inline": "ctx._source.hits_history += 0;"
> },
> "query": {
> "match_all": {}
> }
> }
> '
> 
> ```

Result:

> ```
> "type" : "script_exception",
> "reason" : "runtime error",
> "caused_by" : {
> "type" : "class_cast_exception",
> "reason" : "Cannot apply [+] operation to types [java.util.ArrayList] and [java.lang.Integer]."
> 
> ```

So, how can I append items to the arrayList? Is there a more up-to-date documentation I should look into?

What I wanted to do was simply something like this:

> `ctx._source.hits_history.add(ctx._source.today_hits); ctx._source.today_hits = 0;`

Thank you

---

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 1:05pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/2 "2017-04-18T13:05:11Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 4:49pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/3 "2017-04-18T16:49:47Z")

</div>

tried:  
.addAll([params.hits])  
.add(0, params.hits) (since it says on painless API reference that .add arg 0 is the index)

+= [params.hits]

but to no avail.

PLEASE can someone let me know how to make this work? I'm in a tight schedule

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 18, 2017, 4:58pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/4 "2017-04-18T16:58:21Z")

</div>

Works for me (running on master branch):

```
DELETE test
POST test/doc
{
  "hits_history": [0,0]
}
POST /test/doc/_update_by_query?pretty
	{
	  "script": {
		"inline": "ctx._source.hits_history.add(params.hits)",
		"params": {"hits": 1}
	  },
	  "query": {
		"match_all": {}
		}
	}
GET test/doc/_search
```

---

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 4:59pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/5 "2017-04-18T16:59:43Z")

</div>

hey Mark, what's your mapping for the test doc?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 18, 2017, 5:02pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/6 "2017-04-18T17:02:10Z")

</div>

```
{
  "test": {
	"mappings": {
	  "doc": {
		"properties": {
		  "hits_history": {
			"type": "long"
		  }
		}
	  }
	}
  }
}
```

---

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 5:25pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/7 "2017-04-18T17:25:07Z")

</div>

ok, just tested with "hits" = 1 (like yours) and it is working. Crazy stuff.

---

<div class="post-metadata">

**Author:** ![dwjorgeb](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@dwjorgeb](https://discuss.elastic.co/u/dwjorgeb)\
**Post date:** [April 18, 2017, 11:41pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/8 "2017-04-18T23:41:46Z")

</div>

found the bug, turns out one of the items had hits\_history as an integer instead of an array ( = 0, instead of = [0]), so it was bugging the whole system

thank you for the reply anyway!

---

<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:** [May 16, 2017, 11:56pm UTC](https://discuss.elastic.co/t/append-to-a-array-field/82649/9 "2017-05-16T23:56:42Z")

</div>

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