0

How to update Elasticsearch data multiple fields using UpdateByQuery in NodeJS ?

Note - My data is coming dynamically. I can't pass static value. I have to pass like - data.name, data.id

Code -

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

Error -

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

Please let me know, if any other options are there. I can't update using id, because I don't know the id. I wanted to use updateByQuery, with conditions in the query parameters.

1 Answers1

0

Here are the solutions -

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