arif
arif

Reputation: 669

Elasticsearch 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
}

Upvotes: 0

Views: 350

Answers (1)

Val
Val

Reputation: 217304

It's the same issue as described here, i.e. the single quotes around arif conflict with the single quotes around the JSON query.

So you can either send your query in binary mode as explained in the link above, or escape the quotes, like this:

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\";",         <---- escape quotes
    "lang": "painless"       
  }, 
  "query": {
    "term": {
      "userip": "10.0.30.181"
    }
  }
}
'

Upvotes: 1

Related Questions