Home > Blockchain >  Elastic Search update by query script for a string field
Elastic Search update by query script for a string field

Time:04-22

Unable to do Elastic Search update by query script for a string field. This is my query

curl -X POST "http://localhost:9200/z5-purchase-orders/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
    "script": {
        "source": "ctx._source.facility_name = VCU",
        "lang": "painless"
    },
    "query": {
        "bool" : {
            "must" : [
                { "match" : {"facility_id" : 0} },
                { "match" : {"customer_id" : 1002} }
            ]
        }
    }
}
'

I did the same query for integer field. It worked fine, with string its causing error

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... ._source.facility_name = VCU",
          "                             ^---- HERE"
        ],
        "script" : "ctx._source.facility_name = VCU",
        "lang" : "painless"
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "... ._source.facility_name = VCU",
      "                             ^---- HERE"
    ],
    "script" : "ctx._source.facility_name = VCU",
    "lang" : "painless",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Variable [VCU] is not defined."
    }
  },
  "status" : 400
}

Please help with this string update by query. I use elastic search version 7.

CodePudding user response:

To fix that error, you simply need to stringify the value VCU

"ctx._source.facility_name = 'VCU'"

If you still get an error after that, it's going to be a different one, feel free to share it.

CodePudding user response:

After escaping the characters it worked fine.

"script": {
                "source": "ctx._source.facility_name = \"VCU\"",
                "lang": "painless"
            },
  • Related