簡體   English   中英

如何在 NodeJS 中使用 UpdateByQuery 更新 Elasticsearch 動態數據多個字段

[英]How to update Elasticsearch dynamic data multiple fields using UpdateByQuery in NodeJS

如何在 NodeJS 中使用 UpdateByQuery 更新 Elasticsearch 數據多個字段?

注意 - 我的數據是動態的。 我無法傳遞 static 值。 我必須通過 - data.name, data.id

代碼 -

function updateInELK(data) { // Update by Id
    const updateScript = {
        inline: {
           "ctx._source.name = "+data.name,
           "ctx._source.role = "+data.role,
    };
    return new Promise((resolve, reject) => {
         elasticsearch.updateByQuery({
             index: indexName,
             body: {
                query: { match: { id: data.id } },
                script: updateScript,
                lang: 'painless',
             }
         }).then((response) => {
             resolve(response);
         }).catch((err) => {
             console.log(err);
             reject("Elasticsearch ERROR - data not updated")
         }) 
     });
}

錯誤 -

TypeError: "ctx._source.name = " is not a function 

如果還有其他選擇,請告訴我。 我無法使用 id 進行更新,因為我不知道 id。 我想使用 updateByQuery,在查詢參數中包含條件。

這是解決方案-

await esClient.updateByQuery({
    index: "data",
    type: "doc",
    refresh: true,
    body:{
        query:{
            match: {
                dataId: "70897e86-9d69-4700-b70e-881a7f74e9f9"
            }
        },
        script:{
            lang:"painless",
            source:`ctx._source.data='This is updated test data';ctx._source.updatedAt=params.date;ctx._source.segmentId=params.segmentId`,
            params:{
                date: Date.now(),
                segmentId: null
            }
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM