Looking for Elasticsearch updateByQuery syntax example (Node driver)

17,597

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

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

Solution 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"
      }
    }
  }
});
Share:
17,597
James Jensen
Author by

James Jensen

Updated on June 17, 2022

Comments

  • James Jensen
    James Jensen almost 2 years

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

  • James Jensen
    James Jensen over 7 years
    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):
  • Val
    Val over 7 years
    Ok, then it should work provided you have a compatible ES version (<=1.7 or >=2.3)
  • James Jensen
    James Jensen over 7 years
    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'" } },
  • Val
    Val over 7 years
    Which exact version of ES are you using ? 2.3.2 or 2.3.3 ?
  • James Jensen
    James Jensen over 7 years
    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_BbFv
  • Val
    Val over 7 years
    Great to hear it was just a version issue. Let us know!