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

James Jensen
  • 255
  • 1
  • 2
  • 11

3 Answers3

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)
        }
    )
Community
  • 1
  • 1
James Jensen
  • 255
  • 1
  • 2
  • 11
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)
        }
    )
Val
  • 207,596
  • 13
  • 358
  • 360
  • 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): – James Jensen Aug 24 '16 at 04:59
  • Ok, then it should work provided you have a compatible ES version (<=1.7 or >=2.3) – Val Aug 24 '16 at 05:02
  • 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'" } }, – James Jensen Aug 24 '16 at 05:31
  • Which exact version of ES are you using ? 2.3.2 or 2.3.3 ? – Val Aug 24 '16 at 05:33
  • 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: https://kobra.io/#/e/-KPuwAytjSUkU5K_BbFv – James Jensen Aug 24 '16 at 05:40
  • Great to hear it was just a version issue. Let us know! – Val Aug 24 '16 at 05:41
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"
      }
    }
  }
});
glerendegui
  • 1,457
  • 13
  • 15