How to escape colon in painless

Hi to everyone!

Few days ago someone in my company has created in my cluster a field called total_chunks.RS|24|12:24 and I would like to rename It with an update_by_query.

I have tried this query:


{
  "query": { 
    "bool": {
        "must": {
            "exists": {
                "field": "body.total_chunks.RS|24|12:24"
            }
        }
    }
  },
  "script" : {
    "inline": """ ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12:24; \n ctx._source.remove(\"body.total_chunks.RS|24|12:24\");"""
  }
}

And returns to me this error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... ody.total_chunks.RS|24|12:24; \\n ctx._source.remov ...",
          "                             ^---- HERE"
        ],
        "script" : " ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12:24; \\n ctx._source.remove(\\\"body.total_chunks.RS|24|12:24\\\");",
        "lang" : "painless",
        "position" : {
          "offset" : 83,
          "start" : 58,
          "end" : 108
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "... ody.total_chunks.RS|24|12:24; \\n ctx._source.remov ...",
      "                             ^---- HERE"
    ],
    "script" : " ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12:24; \\n ctx._source.remove(\\\"body.total_chunks.RS|24|12:24\\\");",
    "lang" : "painless",
    "position" : {
      "offset" : 83,
      "start" : 58,
      "end" : 108
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "unexpected token [':'] was expecting one of [{<EOF>, ';'}]."
    }
  },
  "status" : 400
}

So I thought to escape colon, but if I insert a backslash, another error occurs:

This is the inline script with the escaped colon
""" ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12\:24; \n ctx._source.remove(\"body.total_chunks.RS|24|12:24\");"""

And this is the new error, that says that backslash was unexpected:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... ody.total_chunks.RS|24|12\\:24; \\n ctx._source.remo ...",
          "                             ^---- HERE"
        ],
        "script" : " ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12\\:24; \\n ctx._source.remove(\\\"body.total_chunks.RS|24|12:24\\\");",
        "lang" : "painless",
        "position" : {
          "offset" : 83,
          "start" : 58,
          "end" : 108
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "... ody.total_chunks.RS|24|12\\:24; \\n ctx._source.remo ...",
      "                             ^---- HERE"
    ],
    "script" : " ctx._source.body.total_chunks.RS_24_12_24 = ctx._source.body.total_chunks.RS|24|12\\:24; \\n ctx._source.remove(\\\"body.total_chunks.RS|24|12:24\\\");",
    "lang" : "painless",
    "position" : {
      "offset" : 83,
      "start" : 58,
      "end" : 108
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "unexpected character [\\].",
      "caused_by" : {
        "type" : "lexer_no_viable_alt_exception",
        "reason" : null
      }
    }
  },
  "status" : 400
}

What I'm doing wrong here?

Thank you in advance

I tried it also and not sure what's going on with the :. But you can do this using an ingest processor like so.

DELETE testcolon

PUT testcolon

POST testcolon/_doc
{
  "total_chunks.RS|24|12:24": "test"
}

PUT _ingest/pipeline/colon
{
  "processors": [
    {
      "dot_expander": {
        "field": "total_chunks.RS|24|12:24"
      }
    },
    {
      "rename": {
        "field": "total_chunks.RS|24|12:24",
        "target_field": "total_chunks_RS_24_12_24"
      }
    },
    {
      "remove": {
        "field": "total_chunks"
      }
    }
  ]
}

POST testcolon/_update_by_query?pipeline=colon

GET testcolon/_search

Results

"_source" : {
  "total_chunks_RS_24_12_24" : "test"
}
1 Like

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