How to replace string without regexp inside Painless inline script for AWS ElasticSearch?

This is a copy from the StackOverflow question:

The type of the "level" field in the document was changed from "keyword" to "short" and I'm trying to reindex exist data to be able to use it in Kibana charts.
Old data contains values like: "100%", "error" or just empty string "".

I want to get only integer inside new index. I use internal reindex API (new lines added to make a snippet more readable):

curl -s -X POST -H 'Content-Type: application/json' -d '{
  "source": {
    "index": "old-index"
  "dest": {
    "index": "new-index"
  "script": {
    "inline": "
        if (ctx._source.level == \"error\" || ctx._source.level == \"\")
            ctx._source.level = -1
        } else {
            ctx._source.level = Integer.valueOf(ctx._source.level)    )

But I'm getting the error: "java.lang.String cannot be cast to java.lang.Number" because of the "%" symbol at the end of a value.

Also I don't have regular expressions enabled for AWS ElasticSearch and it's not possible to do as I think. So the variant with replaceAll doesn't work for me. If I have self-hosted ES, for example it could be something like this (didn't test it): /(%)?/.matcher(doc['level'].value).replaceAll('$1'):

But from AWS ES I see this:

Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep recursion and long loops.

Is it possible to replace string with Painless language without regexp?

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