ElasticSearch =>如何使用update_by_query

时间:2018-10-10 02:20:31

标签: elasticsearch

我想更新我的名字为wang的索引中的数据。 我的索引代码如下:

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

我的更新请求如下:

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

但是我遇到了这样的错误:

{
  "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
}

所以我想知道在使用“ update_by_query”时如何实现这一点

1 个答案:

答案 0 :(得分:0)

我认为这对您有用,只需将doc部分替换为script。如果inline为您显示已弃用,则只需使用source

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