wangjinhao
wangjinhao

Reputation: 123

ElasticSearch => How to updates with a partial document using update_by_query

I want to update the data in my index whose cname is wang. My index code is as follows:

PUT index_c
{
   "mappings": {
      "_doc" : {
         "properties" : {
            "cid" : {
               "type" : "keyword"
            },
            "cname" : {
               "type" : "keyword"
            },
            "cage" : {
               "type" : "short"
            },
            "chome" : {
               "type" : "text"
            }
         }
      }
   }
}

And my update request is as follows:

POST index_c/_update_by_query
{
   "query" : {
      "match": {
        "cname": "wang"
      }
   },
   "doc" : {
      "cage" : "100",
      "chome" : "china"
   }
}

But I got an error like this:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "Unknown key for a START_OBJECT in [doc].",
        "line": 1,
        "col": 43
      }
    ],
    "type": "parsing_exception",
    "reason": "Unknown key for a START_OBJECT in [doc].",
    "line": 1,
    "col": 43
  },
  "status": 400
}

So I want to know how to implement this when using "update_by_query"

Upvotes: 5

Views: 1827

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

I think this will work for you just replace the doc part with script. if inline shows deprecated for you then just use source instead

POST index_c/_update_by_query
{
    "query" : {
      "match": {
        "cname": "wang"
      }
   },
   "script" : {
      "inline" : "ctx._source.cage='100'; ctx._source.chome= 'china';", 
      "lang"   : "painless"
   } 
}

Upvotes: 2

Related Questions