简体   繁体   English

更新Elasticsearch中的多个文档?

[英]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. 我基本上希望将“结果”分解为100个文档的块,并在单个请求中更新一个块。

You can do it like this instead, ie first create all the bulk commands and then invoke the _bulk endpoint: 您可以改为这样,即首先创建所有批量命令,然后调用_bulk端点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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