【问题标题】:Looking for Elasticsearch updateByQuery syntax example (Node driver)寻找 Elasticsearch updateByQuery 语法示例(节点驱动程序)
【发布时间】:2019-11-23 22:47:57
【问题描述】:

您有一个包含两个文档的 Elasticsearch 索引:

[
  {
    "_index": "myIndex",
    "_type": "myType",
    "_id": "es1472002807930",
    "_source": {
      "animal": "turtle",
      "color": "green",
      "weight": 20,
    }
  },
  {
    "_index": "myIndex",
    "_type": "myType",
    "_id": "es1472002809463",
    "_source": {
      "animal": "bear",
      "color": "brown"
      "weight": 400,
    }
  }
]

稍后,您将获得有关熊的更新数据:

{
  "color": "pink",
  "weight": 500,
  "diet": "omnivore",
}

因此,您想更新熊的“颜色”和“体重”值,并将“饮食”键添加到“熊”文档中。您知道只有一个带有"animal": "bear" 的文档(但您不知道_id):

使用 Nodejs 驱动程序,什么 updateByQuery 语法会用这些新值更新“熊”文档?

(注意:此问题已被完全编辑,以便对 SO 社区更有用!)

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0


    【解决方案1】:

    Val 在另一个 SO 中提供了答案:

    How to update a document based on query using elasticsearch-js (or other means)?

    答案如下:

        var theScript = {
            "inline": "ctx._source.color = 'pink'; ctx._source.weight = 500; ctx._source.diet = 'omnivore';"
        }
    
        client.updateByQuery({ 
               index: myindex,
               type: mytype,
               body: { 
                  "query": { "match": { "animal": "bear" } }, 
                  "script": theScript
               }
            }, function(err, res) { 
                if (err) { 
                   reportError(err) 
                } 
                cb(err, res)
            }
        )
    

    【讨论】:

      【解决方案2】:

      另一个答案没有抓住重点,因为它没有任何脚本来执行更新。

      你需要这样做:

      POST /myIndex/myType/_update_by_query
      {
        "query": { 
          "term": {
            "animal": "bear"
          }
        },
        "script": "ctx._source.color = 'green'"
      }
      

      重要提示:

      • 您需要确保enable dynamic scripting 才能正常工作。
      • 如果您使用的是 ES 2.3 或更高版本,则内置查询更新功能
      • 如果您使用的是 ES 1.7.x 或之前的版本,您需要安装 update-by-query plugin
      • 如果您使用的是 ES 2.0 和 2.2 之间的任何东西,那么您没有办法一次性完成,您需要分两次操作。

      更新

      您的 node.js 代码应如下所示,您缺少 body 参数:

          client.updateByQuery({ 
                 index: index,
                 type: type,
                 body: { 
                    "query": { "match": { "animal": "bear" } }, 
                    "script": { "inline": "ctx._source.color = 'pink'"}
                 }
              }, function(err, res) { 
                  if (err) { 
                     reportError(err) 
                  } 
                  cb(err, res)
              }
          )
      

      【讨论】:

      • 我启用了动态脚本 我正在使用 ES 2.3 我正在使用 Nodejs 驱动程序库 当我执行时:client.updateByQuery({ index: 'myIndex', type: 'myType', "query ": { "match": { "animal": "bear" } }, "script": "ctx._source.color = 'green'" }, function(err, res) { if (err) { reportError(err ) } }) 我收到此错误(在以下评论中):
      • 好的,如果你有兼容的 ES 版本(=2.3),它应该可以工作
      • 奇怪的是,该命令在索引中创建了第三个文档:{“_index”:“myindex”,“_type”:“mytype”,“_id”:“_update_by_query”,“_score”:1 , "_source": { "query": { "match": { "animal": "bear" } }, "script": "ctx._source.color = 'green'" } },
      • 您使用的是哪个版本的 ES? 2.3.2 还是 2.3.3 ?
      • 啊,版本是2.2.0。我会尝试升级。另外,非常感谢您的帮助,我为格式道歉,这看起来更好:kobra.io/#/e/-KPuwAytjSUkU5K_BbFv
      【解决方案3】:

      对于 elasticsearch 7.4,您可以使用

      await client.updateByQuery({
        index: "indexName",
        body: {
          query: {
            match: { fieldName: "valueSearched" }
          },
          script: {
            source: "ctx._source.fieldName = params.newValue",
            lang: 'painless',
            params: {
              newValue: "newValue"
            }
          }
        }
      });
      

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 2017-01-26
        • 2017-12-18
        • 2015-05-29
        • 2014-11-20
        • 1970-01-01
        • 2012-02-26
        • 2020-03-27
        • 1970-01-01
        相关资源
        最近更新 更多