简体   繁体   中英

update multiple documents in elasticsearch?

In my current code, I fetch multiple documents and update then one by one, as following:

results.hits.hits.forEach(function (hit) {
             hit._source.taskName = 'My-task-name';
             esClient.bulk({
                 body: [
                     {update: {_index: 'my-index', _type: 'default', _id: hit._id}},
                     {doc: hit._source}
                 ]
             }, function (err, resp) {
                 // ...
                 if (err) {
                     console.log(err);
                 }
                 if(resp) {
                     console.log(resp)
                 }
             });
         });

This approach has timeout issue.

So I want to update multiple documents in a single request. But I am not sure how to construct the body for it, send request asynchronously and then how to deal with the response. I would basically want to break up 'results' in chunks of, say 100 documents, and update a chunk in single request.

You can do it like this instead, ie first create all the bulk commands and then invoke the _bulk endpoint:

const bulk = [];
results.hits.hits.forEach(function (hit) {
    hit._source.taskName = 'My-task-name';
    bulk.push({update: {_index: 'my-index', _type: 'default', _id: hit._id}});
    bulk.push({doc: hit._source});
});

esClient.bulk({
    body: bulk
}, function (err, resp) {
    // ...
    if (err) {
        console.log(err);
    }
    if(resp) {
        console.log(resp)
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM