Elasticsearch: painless cannot parse escaped characters correctly

Created on 13 Dec 2017  路  3Comments  路  Source: elastic/elasticsearch

Describe the feature:

Elasticsearch version (bin/elasticsearch --version):5.6.3

Plugins installed: [smartcn ]

JVM version (java -version):
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

OS version (uname -a if on a Unix-like system):
CentOS Linux release 7.2.1511 (Core)

Steps to reproduce:

  1. insert a new doc.
curl -XPUT 'http://192.168.40.106:9200/janusgraph_vertices/vertices/1111' -d '{ "Person_emailList":["[email protected]"] }'

2.update multi value field via painless script.

curl -XPOST '192.168.40.106:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{"update":{"_index":"janusgraph_vertices","_type":"vertices","_id":"1111"}}
{"script":{"source":"if(ctx._source[\"Person_emailList\"] == null){ctx._source[\"Person_emailList\"] = [];}ctx._source[\"Person_emailList\"].add(\"\\\"[email protected]\\t\");","lang":"painless"},"upsert":{"Person_emailList":["\"[email protected]\t"]}}
'

Provide logs (if relevant):

{
  "took" : 94,
  "errors" : true,
  "items" : [
    {
      "update" : {
        "_index" : "janusgraph_vertices",
        "_type" : "vertices",
        "_id" : "1111",
        "status" : 400,
        "error" : {
          "type" : "illegal_argument_exception",
          "reason" : "failed to execute script",
          "caused_by" : {
            "type" : "script_exception",
            "reason" : "compile error",
            "script_stack" : [
              "... [\"Person_emailList\"].add(\"\\\"[email protected]\\t\");",
              "                             ^---- HERE"
            ],
            "script" : "if(ctx._source[\"Person_emailList\"] == null){ctx._source[\"Person_emailList\"] = [];}ctx._source[\"Person_emailList\"].add(\"\\\"[email protected]\\t\");",
            "lang" : "painless",
            "caused_by" : {
              "type" : "illegal_argument_exception",
              "reason" : "unexpected character [\"\\\"[email protected]\\t]. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].",
              "caused_by" : {
                "type" : "lexer_no_viable_alt_exception",
                "reason" : null
              }
            }
          }
        }
      }
    }
  ]
}
:CorInfrScripting

Most helpful comment

Painless does not have escape sequences really.

This is really painful.

All 3 comments

Painless does not have escape sequences really. As the error message explains, when inside a double quoted string, the only valid escapes are backslash and double quote. But your string has an escaped tab. All you should need to do is remove the double slash before t. Also note that you can use single quoted strings in painless to lessen the need for escaping json strings.

"source":"if(ctx._source['Person_emailList'] == null){ctx._source['Person_emailList'] = [];}ctx._source['Person_emailList'].add('\"[email protected]\t');"

In the future, please ask questions on our forum. We use github for feature requests and confirmed bug reports.

Painless does not have escape sequences really.

This is really painful.

easy fix : String.valueOf() ...

Was this page helpful?
0 / 5 - 0 ratings