我正在使用elasticsearch
npm 库。
我使用批量 API 为文档编制索引:
const response = await this.elasticSearchClient.bulk({
body: bulkRows,
});
它有效。
但是,如果 ElasticSearch 在某些索引中失败,我如何从响应中检测到这些失败?
我在文档 (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html( 中找到了一个示例响应,但我仍然不明白如何检测故障:
您可以通过检查从批量调用中获得的响应来执行此操作:
const { body: bulkResponse } = await this.elasticSearchClient.bulk({ body: bulkRows })
if (bulkResponse.errors) {
const erroredDocuments = []
bulkResponse.items.forEach((action, i) => {
const operation = Object.keys(action)[0]
if (action[operation].error) {
erroredDocuments.push({
// If the status is 429 it means that you can retry the document,
// otherwise it's very likely a mapping error, and you should
// fix the document before to try it again.
status: action[operation].status,
error: action[operation].error,
operation: body[i * 2],
document: body[i * 2 + 1]
})
}
})
console.log(erroredDocuments)
}