Looking for Elasticsearch updateByQuery syntax example (Node driver)

瘦欲@ 提交于 2019-12-01 18:42:05

The other answer is missing the point since it doesn't have any script to carry out the update.

You need to do it like this:

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

Important notes:

  • you need to make sure to enable dynamic scripting in order for this to work.
  • if you are using ES 2.3 or later, then the update-by-query feature is built-in
  • if you are using ES 1.7.x or a former release you need to install the update-by-query plugin
  • if you are using anything between ES 2.0 and 2.2, then you don't have any way to do this in one shot, you need to do it in two operations.

UPDATE

Your node.js code should look like this, you're missing the body parameter:

    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)
        }
    )
James Jensen

The answer was provided by Val in this other SO:

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

Here is the answer:

    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)
        }
    )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!