elasticsearchupdate-by-query

Elastic update by Query - How to update a field with digits as a string


Okay so the problem is: I want to add a field to my document in Elastic, with a spefic writing of the value (so R can read the field)

The field must look like this:

         "dbas_station_regex": """^(GW\d{5})_""",

(Some documents already have the field, and I want to update the docs with the missing value)

So I try these:

POST my_index/_update_by_query
{
  "query": {
    "term": {
      "_id": {
        "value": "bIRukokBcjCrX8D7Tn2D"
      }
    }
  },
  "script": {
    "params": {
      "fieldToAdd": "dbas_station_regex",
      "newValue": "^(GW\\d{5})_"
    },
    "source": "ctx._source[params.fieldToAdd] = [params.newValue]",
    "lang": "painless"
  }
}

But the response was this:

"dbas_station_regex": [
            """^(GW\d{5})_"""
          ],

But R don´t like this:

Error in `stringr::str_match()` at ntsportal/R/add_rawfiles.R:103:3:
! `pattern` must be a string, not a list.

I don´t understand why elastic makes a list here, but okay.

Next I try:

POST my_index/_update_by_query
{
  "query": {
    "term": {
      "_id": {
        "value": "bIRukokBcjCrX8D7Tn2D"
      }
    }
  },
  "script" : "ctx._source.dbas_station_regex = '^(GW\\d{5})_'"
}

and I got a Error:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          """... rce.dbas_station_regex = '^(GW\d{5})_'""",
          "                             ^---- HERE"
        ],
        "script": """ctx._source.dbas_station_regex = '^(GW\d{5})_'""",
        "lang": "painless",
        "position": {
          "offset": 33,
          "start": 8,
          "end": 46
        }
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      """... rce.dbas_station_regex = '^(GW\d{5})_'""",
      "                             ^---- HERE"
    ],
    "script": """ctx._source.dbas_station_regex = '^(GW\d{5})_'""",
    "lang": "painless",
    "position": {
      "offset": 33,
      "start": 8,
      "end": 46
    },
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": """unexpected character ['^(GW\d]. The only valid escape sequences in strings starting with ['] are [\\] and [\'].""",
      "caused_by": {
        "type": "lexer_no_viable_alt_exception",
        "reason": null
      }
    }
  },
  "status": 400
}

How can I fix this problem?

(And the mapping of the field is: keyword)


Solution

  • I think you define the new value as an array:

    Here:

    "source": "ctx._source[params.fieldToAdd] = [params.newValue]",
    

    It should be:

    "source": "ctx._source[params.fieldToAdd] = params.newValue",
    

    as stated here: https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-update-by-query-context.html