# Elasticsearch painless updating List\<String\> in JAVA not working

**URL:** https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232
**Category:** Elasticsearch
**Created:** [December 27, 2018, 12:32pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232 "2018-12-27T12:32:33Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [December 27, 2018, 12:32pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/1 "2018-12-27T12:32:33Z")

</div>

When I am updating the single String in painless, its working fine. But when updating List in painless is not working.

Any help would be appreciated.

Regards

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 27, 2018, 2:12pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/2 "2018-12-27T14:12:09Z")

</div>

Can you show a simple script that reproduces the issue?

---

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [December 30, 2018, 5:36am UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/3 "2018-12-30T05:36:44Z")

</div>

Both of the below scripts are not working.

String scriptAdd = "List addValues = params.add\_list; ctx.\_source.labels.addAll(addValues);";

String scriptRemove = "List removeValues = params.remove\_list; ctx.\_source.labels.removeAll(removeValues);";

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [December 31, 2018, 9:42pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/4 "2018-12-31T21:42:17Z")

</div>

Could you modify this script to reproduce the issue. It seem to work for me:

```auto
DELETE test

PUT test/doc/1
{
  "labels": ["0"]
}

POST test/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test": {
      "script": {
        "inline": "List addValues = params.add_list; ctx._source.labels.addAll(addValues);",
        "params": {
          "add_list": ["1", "2", "3"]
        }
      }
    }
  }
}

POST test/doc/1/_update
{
  "script": {
    "inline": "List addValues = params.add_list; ctx._source.labels.addAll(addValues);",
    "params": {
      "add_list": [
        "1",
        "2",
        "3"
      ]
    }
  }
}

GET test/doc/1

```

It returns

```auto
{
  "_index": "test",
  "_type": "doc",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "labels": [
      "0",
      "1",
      "2",
      "3"
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [January 1, 2019, 12:56pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/5 "2019-01-01T12:56:20Z")

</div>

Thanks for your reply.

What you sent is working fine in HTTP API of elastic search.

Even below code is working fine  
POST test/doc/1/\_update  
{  
"script": {  
"inline": "List addValues = params.add\_list; for(int i=0; i \< addValues.size(); i++) { if(!ctx.\_source.labels.contains(addValues.get(i))) ctx.\_source.labels.add(addValues.get(i)); }",  
"params": {  
"add\_list": [  
"1",  
"2",  
"3"  
]  
}  
}  
}

But I am using JAVA high level API for Elastic search . I think problem is in JAVA API

1. String code = "def addValues = params.add\_list; ctx.\_source.labels.addAll(addValues);";  
Working perfectly fine

2. Script{type=inline, lang='painless', idOrCode=' ArrayList addValues = params.add\_list; for(int i=0;i\<addValues.size();i++) {if(!ctx.\_source.labels.contains(addValues.get(i)) ctx.\_source.labels.add(addValues.get(i));}', options={}, params={remove\_list=[Label 1, Item 2, multiple, sent, trash], add\_list=[ss\_label2, ss\_label4, ne\_label1, ss\_label3, inbox]}}

Exception::\>  
ElasticsearchStatusException[Elasticsearch exception [type=script\_exception, reason=compile error]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=invalid sequence of tokens near ['\<'].]]; nested: ElasticsearchException[Elasticsearch exception [type=no\_viable\_alt\_exception, reason=null]];

1. Script{type=inline, lang='painless', idOrCode='List addValues = params.add\_list; for(int i=0;i\<addValues.size();i++) {if(!ctx.\_source.labels.contains(addValues.get(i)) ctx.\_source.labels.add(addValues.get(i));}', options={}, params={remove\_list=[label4, ss\_label2, sent, inbox, label1, label2], add\_list=[label4, ss\_label2, ss\_label1, ss\_label4, ss\_label3, inbox]}}

Exception::\>  
ElasticsearchStatusException[Elasticsearch exception [type=script\_exception, reason=compile error]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=invalid sequence of tokens near ['\<'].]]; nested: ElasticsearchException[Elasticsearch exception [type=no\_viable\_alt\_exception, reason=null]];

1. Script{type=inline, lang='painless', idOrCode='def addValues = params.add\_list; for(int i=0;i\<addValues.size();i++) {if(!ctx.\_source.labels.contains(addValues.get(i)) ctx.\_source.labels.add(addValues.get(i));}', options={}, params={remove\_list=[label4, ss\_label2, sent, inbox, label1, label2], add\_list=[label4, ss\_label2, ss\_label1, ss\_label4, ss\_label3, inbox]}}

Exception::\>  
ElasticsearchStatusException[Elasticsearch exception [type=script\_exception, reason=compile error]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal\_argument\_exception, reason=unexpected token ['ctx'] was expecting one of [')'].]];

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [January 2, 2019, 3:50pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/6 "2019-01-02T15:50:43Z")

</div>

I cannot reproduce the issue for 2 and 3, but in all of them you are missing closing ')' in the `if` statement.

```auto
   def addValues = params.add_list; 
   for(int i=0;i&lt;addValues.size();i++) {
     if(!ctx._source.labels.contains(addValues.get(i)) <--- Missing ')' 
        ctx._source.labels.add(addValues.get(i));
  }

```

---

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [January 22, 2019, 12:41pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/7 "2019-01-22T12:41:30Z")

</div>

Thanks for your reply,  
But below script is not working. Please let me know the solution:

code = "ctx.\_source.attachments.add([" +  
 ""attachmentId" : params.attachmentId," +  
 ""size" : params.size," +  
 ""type" : params.type," +  
 ""file\_name" : params.file\_name" +  
 "]);";

---

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [January 22, 2019, 12:51pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/8 "2019-01-22T12:51:30Z")

</div>

Following below link:

> <https://stackoverflow.com/questions/49377632/how-to-create-and-add-nested-object-into-nested-field-in-elasticsearch/49378001#49378001>

---

<div class="post-metadata">

### Author: ![Rashid\_Yes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashid_yes/32/45796_2.png) [@Rashid\_Yes](https://discuss.elastic.co/u/Rashid_Yes)
#### Post date: [January 23, 2019, 12:58pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/9 "2019-01-23T12:58:47Z")

</div>

Below is working fine:

{  
"script": {  
"inline": "ctx.\_source.attachments.add(["first" : params.first, "last" : params.last]);",  
"params": {  
"first": "123", "last": "456"  
}  
}  
}

---

<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 20, 2019, 12:58pm UTC](https://discuss.elastic.co/t/elasticsearch-painless-updating-list-string-in-java-not-working/162232/10 "2019-02-20T12:58:47Z")

</div>

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