es 批量更新字段值操作

官方文档

curl -X POST "localhost:9200/twitter/_update_by_query" -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}
'

具体使用

方式一:
{
  "script": {
    "source": "ctx._source.xxx='xxxxx'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "xxxx": "xxxxx"
    }
  }
}
方式二:
{
  "script": {
    "source": "ctx._source['xxx']='xxxxx'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "xxxx": "xxxxx"
    }
  }
}

以上2种 query 都会报错
报错如下:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "ctx._source[xxx]=xxxx",
          "            ^---- HERE"
        ],
        "script": "ctx._source[xxx]=xxxx",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "ctx._source[xxx]=xxxx",
      "            ^---- HERE"
    ],
    "script": "ctx._source[xxx]=xxxx",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Variable [xxx] is not defined."
    }
  },
  "status": 500
}
  • 原因:es 不同的版本可以使用的语法稍有不同

修改如下:

curl -X POST "http://127.0.0.1:9200/indexxxx/_update_by_query" -H 'Content-Type: application/json' -d'
{
  "script": {
      "source": "ctx._source.name = params.xxxname",
      "params": { "xxxname": "abcabc" },
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
    }
  }
}
'

你可能感兴趣的:(程序人生,es,elasticsearch,update)