Updating field value issue

I am trying to work with update_by_query but cannot make it work.

Following is a simple query,

curl -X GET "172.17.0.3:9200/useripvsuserid/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "_source":"userid","query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 10.803431,
    "hits" : [
      {
        "_index" : "useripvsuserid",
        "_type" : "_doc",
        "_id" : "PhfBW3AB8mhGfmGvIs-j",
        "_score" : 10.803431,
        "_source" : {
          "userid" : "hasan1855"
        }
      }
    ]
  }
}

Following is the update_by_query that is not working. I am trying to replace userid value hasan1855 to arif. Where is the problem?

curl -X POST "172.17.0.3:9200/useripvsuserid/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {                                                                                                                                                                                                        
    "source": "ctx._source.userid='arif';",
    "lang": "painless"       
  }, 
  "query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "ctx._source.userid=arif;",
          "                   ^---- HERE"
        ],
        "script" : "ctx._source.userid=arif;",
        "lang" : "painless"
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "ctx._source.userid=arif;",
      "                   ^---- HERE"
    ],
    "script" : "ctx._source.userid=arif;",
    "lang" : "painless",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Variable [arif] is not defined."
    }
  },
  "status" : 400
}

Solved it by escaping quotes in script section like the following,

          "ctx._source.userid=\"arif\";",

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