Elasticsearch Painless: Error when using three quotes: Unexpected Character ('\"' (code 34))
Asked Answered
A

2

7

I'm running a local instance of Elasticsearch, and trying to work with 'painless' under scripted_fields. I can write a single line of script code just fine, but when I use triple-quotes (which is supported as per documentation) to create a multi-line script, it gives me this strange parsing error.

Running a single-line of script works fine:

{
  "script_fields": {
    "scripted": {
      "script": {
        "lang": "painless",
        "source": "0"
      }
    }
  }
}

With this result (expected) in each entity returned in results:

"fields" : {
  "scripted" : [
    0
  ]
}

But using multi-line format:

{
  "script_fields": {
    "scripted": {
      "script": {
        "lang": "painless",
        "source": 
        """
          0
        """
      }
    }
  }
}

Gives me this error:

Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@56e69b76; line: 7, column: 12]

Any ideas?

Amand answered 1/5, 2019 at 2:52 Comment(4)
Which ES version are you running? It works at least on 6.6.1.Concomitant
I'm running 7.0.0. It's so bizarre. Now it's not giving me the error. I tried in Kibana, over curl, and through Postman, all returning correct results now. No idea what was originally causing the issue. :/Amand
Errr, I misspoke. I guess I was using the script above that 'works' haha. Ok, just tried it again through Postman, and same error.Amand
Ok, I'll check on 7 and report backConcomitant
A
7

I was able to work through a solution for Postman (at least). You can't use multi-line strings in a postman body JSON, so use a pre-request script. Here's an example:

pm.environment.set("painless_script",`\
    return 0\
`);

Notes:

  • Use 'ticks' to wrap the script
  • You have to escape the end of every line, otherwise Painless will complain.

Then in the body of the message:

{
  "script_fields": {
    "scripted": {
      "script": {
        "lang": "painless",
        "source": "{{painless_script}}"
      }
    }
  }
}

This yields correct result from Elasticsearch

Amand answered 2/5, 2019 at 14:38 Comment(0)
A
0

Works great, just be sure they are back ticks (`) and not ticks (').

Angelineangelique answered 30/9, 2020 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.