【发布时间】: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