2

I want to perform a update-by-query, specifically update a specific document that where field name that contains Gary. I am using the latest version of elasticsearch (2.3) I am using the official ES client for nodejs. Providing an alternative means to do this (find a document, update a specific field in the document) would be acceptable as a correct answer.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • This has not yet been release. This will be [available in the 2.3 API version](https://github.com/elastic/elasticsearch-js/blob/master/src/lib/apis/2_3.js#L6463-L6709) of the Javascript client library. Right now, the JS client only supports up to `apiVersion: 2.2` – Val May 12 '16 at 05:47
  • I have to update a field for my existing application. Are there any alternative methods I could do to achieve this? – Rolando May 12 '16 at 13:23
  • If it's a one-off need, you can do it with [curl through the REST API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html) instead of using the JavaScript client. – Val May 12 '16 at 13:24
  • What is the nodejs equivalent curl to accomplish this? – Rolando May 12 '16 at 13:25
  • Postman, Sense, /_plugin/head... Any HTTP client will do. You can also use Node's `http` module. – Val May 12 '16 at 13:26
  • 1
    I have already shared an example for ES 7.4 in https://stackoverflow.com/questions/39113370/looking-for-elasticsearch-updatebyquery-syntax-example-node-driver/59008848#59008848 – glerendegui Nov 23 '19 at 14:49
  • @glenrendegui upvoted – Rolando Nov 23 '19 at 14:51

2 Answers2

3

This has not yet been released in the JavaScript client. This will be available in the 2.3 API version of the Javascript client library. Right now, the JS client only supports up to apiVersion: 2.2

You can use any HTTP client (Postman, curl, /head/, Sense, ...) in order to hit the REST endpoint and carry out what you need.

If you do need to do this through Node.js, you can use the http module like this:

var http = require('http');

var options = {
  host: 'localhost',
  port: 9200,
  path: '/your_index/your_type/_update_by_query',
  method: 'POST'
};

var req = http.request(options, function(resp){
  resp.on('data', function(chunk){
    // check update response
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});

var query = {
  "script": {
    "inline": "ctx._source.field = 'value2'"
  },
  "query": {
    "term": {
      "field": "value1"
    }
  }
};

// write data to request body
req.write(JSON.stringify(query));
req.write('\n');
req.end();
Val
  • 207,596
  • 13
  • 358
  • 360
0

I did similar, may helpful for others

async function updateBranches(req){
try{
    let query = '';
    for (const key in req) {
        if (Object.hasOwnProperty.call(req, key)) {
            query = `${query}ctx._source["${key}"] = "${req[key]}";`
        }
    }
    const res = await esclient.updateByQuery({
        index: branchesIndex,
        refresh: true,
        body: {
            query: {
                match: {
                    branchKey: req.branchKey
                }
              },
            script: {
                lang: 'painless',
                source: query
            },
        }
      })
      
    return res;
} catch(e){
    console.log(e,'error');
}}
zulqarnain
  • 1,536
  • 4
  • 26
  • 44