簡體   English   中英

更新Elasticsearch中的多個文檔?

[英]update multiple documents in elasticsearch?

在當前代碼中,我提取了多個文檔,然后一個一個地更新,如下所示:

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

此方法存在超時問題。

因此,我想在一個請求中更新多個文檔。 但是我不確定如何為其構造主體,異步發送請求,然后如何處理響應。 我基本上希望將“結果”分解為100個文檔的塊,並在單個請求中更新一個塊。

您可以改為這樣,即首先創建所有批量命令,然后調用_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