Elasticsearch painless updating List<String> in JAVA not working

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

Can you show a simple script that reproduces the issue?

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);";

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

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

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

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 [')'].]];

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

   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));
  }
1 Like

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" +
"]);";

Following below link:

Below is working fine:

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

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