【问题标题】:How to update Elasticsearch dynamic data multiple fields using UpdateByQuery in NodeJS如何在 NodeJS 中使用 UpdateByQuery 更新 Elasticsearch 动态数据的多个字段
【发布时间】:2023-01-26 13:08:26
【问题描述】:

如何在 NodeJS 中使用 UpdateByQuery 更新 Elasticsearch 数据的多个字段?

注意 - 我的数据是动态的。我不能传递静态值。我必须通过 - 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,在查询参数中包含条件。

【问题讨论】:

    标签: node.js elasticsearch nodejs-server


    【解决方案1】:

    这是解决方案-

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

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2020-01-12
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多