Looking for Elasticsearch updateByQuery syntax example (Node driver)

喜欢而已 提交于 2020-01-10 03:50:07

问题


You have an Elasticsearch index with two docs:

[
  {
    "_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,
    }
  }
]

Later, you get this updated data about the bear:

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

So, you want to update the "color" and "weight" values of the bear, and add the "diet" key to the "bear" doc. You know there's only one doc with "animal": "bear" (but you don't know the _id):

Using the Nodejs driver, what updateByQuery syntax would update the "bear" doc with these new values?

(NOTE: this question has been entirely edited to be more useful to the SO community!)


回答1:


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



回答2:


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



回答3:


For elasticsearch 7.4 you could use

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


来源:https://stackoverflow.com/questions/39113370/looking-for-elasticsearch-updatebyquery-syntax-example-node-driver

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