首页 > 解决方案 > Elasticsearch 批量更新查询?

问题描述

这里阅读本文后,我无法将以下 SQL 转换为弹性 dsl:

UPDATE myIndex
set field1 = staticValue1, field2 = staticValue2
Where {
"query": {
    "bool": {
        "must_not": {
            "exists": {
                "field": "cnpj_estabelecimento"
            }}}}}

我怎样才能做到这一点?

标签: elasticsearch

解决方案


使用Update By Query API时,您有两个部分:

  • 询问。将更新哪些元素。

  • 脚本。会做哪些改变。

对于脚本部分,您将不得不玩一点Painless。不要相信这个名字:学习无痛是痛苦的(一开始)。

现在,对于您的特定情况,您想要做的事情应该如下所示:

  POST /myIndex
  "query" : {
    "bool" : {
      "must_not" : {
        "exists" : {
          {
            "field": "cnpj_estabelecimento"
          }
        }
      }
    }
  },
    "script" : {
      "inline" : "ctx._source.field1= 'staticValue1'; ctx._source.field2= staticValue2;", 
      "lang"   : "painless"
      }
  }

请注意,当您调用 Update By Query API 时,您是通过 POST 而不是 UPDATE 进行的。

希望这有帮助!:D


推荐阅读