在Elasticsearch上按顺序重新索引和删除



我需要重新索引,删除旧索引,并从旧删除的索引重命名为名称,它将在NodeJS中以随机顺序执行,尝试删除和重命名值不存在。

我确实尝试过用promise运行,但没有成功,并且使用了.then(function(resp(,同样的问题也发生了。

es.reindex({
refresh: true,
body: {
"source": {
"index": current_index
},
"dest": {
"index": current_index + ".tmp"
}
}
});
es.indices.delete({
index: current_index,
ignore: [404]
}, function(err, resp, respcode) {
if (err) {
console.log("Error deleting index " + current_index);
console.log("   Error: " + err);
console.log("   Resp code: " + respcode);
}
});
es.reindex({
refresh: true,
body: {
"source": {
"index": current_index + ".tmp"
},
"dest": {
"index": current_index
}
}
});
es.indices.delete({
index: current_index + ".tmp",
ignore: [404]
}, function(err, resp, respcode) {
if (err) {
console.log("Error deleting index " + current_index);
console.log("   Error: " + err);
console.log("   Resp code: " + respcode);
}
});

以下是正确的方法:

const {reindex1Body} = await es.reindex({
refresh: true,
body: {
"source": {
"index": current_index
},
"dest": {
"index": current_index + ".tmp"
}
}
});
try {
await es.indices.delete({
index: current_index
}, {
ignore: [404]
});
} catch (err) {
console.log("Error deleting index " + current_index);
console.log("   Error: " + err);
console.log("   Resp code: " + err.statusCode);
}
const {reindex2Body} = await es.reindex({
refresh: true,
body: {
"source": {
"index": current_index + ".tmp"
},
"dest": {
"index": current_index
}
}
});
try {
await es.indices.delete({
index: current_index + ".tmp"
}, {
ignore: [404]
});
} catch (err) {
console.log("Error deleting index " + current_index);
console.log("   Error: " + err);
console.log("   Resp code: " + err.statusCode);
}

最新更新