Looking for Elasticsearch updateByQuery syntax example (Node driver)
Asked Answered
J

3

14

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

Joaniejoann answered 24/8, 2016 at 2:10 Comment(0)
J
10

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)
        }
    )
Joaniejoann answered 26/8, 2016 at 21:2 Comment(0)
L
8

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)
        }
    )
Lucillelucina answered 24/8, 2016 at 4:3 Comment(6)
I have dynamic scripting enabled I'm using ES 2.3 I'm using the Nodejs driver library When I execute: client.updateByQuery({ index: 'myIndex', type: 'myType', "query": { "match": { "animal": "bear" } }, "script": "ctx._source.color = 'green'" }, function(err, res) { if (err) { reportError(err) } }) I get this error (in the following comment):Joaniejoann
Ok, then it should work provided you have a compatible ES version (<=1.7 or >=2.3)Lucillelucina
Strangely, that command creates a 3rd document in the index: { "_index": "myindex", "_type": "mytype", "_id": "_update_by_query", "_score": 1, "_source": { "query": { "match": { "animal": "bear" } }, "script": "ctx._source.color = 'green'" } },Joaniejoann
Which exact version of ES are you using ? 2.3.2 or 2.3.3 ?Lucillelucina
Ah, the version is 2.2.0. I will try upgrading. Also, thanks so much for your help, and I apologize for the formatting, this looks better: kobra.io/#/e/-KPuwAytjSUkU5K_BbFvJoaniejoann
Great to hear it was just a version issue. Let us know!Lucillelucina
M
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"
      }
    }
  }
});
Murielmurielle answered 23/11, 2019 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.