# Painless script error adding nested items

**URL:** https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896
**Category:** Elasticsearch
**Created:** [January 8, 2017, 7:55pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896 "2017-01-08T19:55:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)
#### Post date: [January 8, 2017, 7:55pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/1 "2017-01-08T19:55:02Z")

</div>

Hi,

I'm having a bit of an issue getting a script working; basically, the script is intended to add a nested item to an existing doc.

To repro, I created an index with this mapping:

```auto
curl -XPUT 'http://localhost:9200/nsttest' 
{
"mappings": 
    { "test": 
        { "properties": 
                { 
                "prntId" : {"type": "keyword", "doc_values": true },
		"kids" : {
		        "type": "nested", 
		        "properties": { 
			                "ctxt": {"type": "keyword", "doc_values": true },
			                "tkn": {"type": "keyword", "doc_values": true }  
			                } 
                                }
                        } 
                }  
        } 
}

```

And added a single doc:

```auto
curl -XPUT 'http://localhost:9200/nsttest/test/abc'
{
  "prntId" : "abc"
}

```

```auto
curl -XPOST 'http://localhost:9200/nsttest/test/abc/_update'
{
	"script": 
	{ 
		"inline" : "if (ctx._source.kids == null || ctx._source.kids.size() == 0){ ctx._source.kids = params.kid} else {ctx._source.kids += params.kid } ", 
		"lang": "painless",
		"params": {"kid": [{"ctxt" : "xs123", "tkn" : "4eef3f46-5469-42d4-91c2-371748d0ee13"}] }
	}
}

curl -XPOST 'http://localhost:9200/nsttest/test/abc/_update/pretty'
{
	"script": 
	{ 
		"inline" : "if (ctx._source.kids == null || ctx._source.kids.size() == 0){ ctx._source.kids = params.kid} else {ctx._source.kids += params.kid } ", 
		"lang": "painless",
		"params": {"kid": [{"ctxt" : "xs1234", "tkn" : "5eef3f46-5469-42d4-91c2-371748d0ee13"}] }
	}
}

```

I have tried the params with and without the square brackets, and basically many variations, and it will work updating a doc for the first time, but not thereafter - i.e. "ctx.\_source.kids += params.kid " always fails...

Here is the error I get:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[bpQJEwv][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "runtime error",
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "Cannot apply [+] operation to types [java.util.ArrayList] and [java.util.HashMap]."
      },
      "script_stack" : [
        "ctx._source.kids += params.kid } ",
        " ^---- HERE"
      ],
      "script" : "if (ctx._source.kids == null || ctx._source.kids.size() == 0){ ctx._source.kids = params.kid} else {ctx._source.kids += params.kid } ",
      "lang" : "painless"
    }
  },
  "status" : 400
}

```

Anyone have any ideas or pointers?

Thanks in advance!

Paul

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [January 8, 2017, 8:22pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/2 "2017-01-08T20:22:52Z")

</div>

Try `ctx._source.kids.add(params.kid)`. `+` only works for math in Painless. I don't recall the justification, but it might be around overhead of doing the extra type checking in the dynamic case or the extra complexity. I'm unsure.

---

<div class="post-metadata">

### Author: ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)
#### Post date: [January 8, 2017, 8:30pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/3 "2017-01-08T20:30:36Z")

</div>

Legend 🙂 Obvious in retrospect, can't believe I didn't see it!

Thanks a bunch - much appreciated!

P

---

<div class="post-metadata">

### Author: ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)
#### Post date: [January 8, 2017, 8:33pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/4 "2017-01-08T20:33:09Z")

</div>

Here's the script in working form, in case anyone is interested - note the square brackets in the params for the case when `ctx._source.kids = params.kid` executes, and the `ctx._source.kids.add(params.kid[0])` for the other case

```auto
{
	"script": 
	{ 
		"inline" : "if (ctx._source.kids == null || ctx._source.kids.size() == 0){ ctx._source.kids = params.kid} else {ctx._source.kids.add(params.kid[0]) } ", 
		"lang": "painless",
		"params": {"kid": [{"ctxt" : "xs123", "tkn" : "4eef3f46-5469-42d4-91c2-371748d0ee13"}] }
	}
}

```

---

<div class="post-metadata">

### Author: ![chetan\_vartak](https://avatars.discourse-cdn.com/v4/letter/c/fbc32d/32.png) [@chetan\_vartak](https://discuss.elastic.co/u/chetan_vartak)
#### Post date: [January 25, 2017, 5:08pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/5 "2017-01-25T17:08:44Z")

</div>

Hi,  
Do you know how to invoke the same painless from logstash. Basically need help on how to pass parameters to painless in logstash.  
There is another question stuck on the same issue - [Update Nested object with logstash](https://discuss.elastic.co/t/update-nested-object-with-logstash/72672)

Thanks

---

<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 22, 2017, 5:08pm UTC](https://discuss.elastic.co/t/painless-script-error-adding-nested-items/70896/6 "2017-02-22T17:08:46Z")

</div>

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