gpt4 book ai didi

javascript - 如何删除elasticsearch索引中的多个id文档

转载 作者:行者123 更新时间:2023-12-01 00:36:27 24 4
gpt4 key购买 nike

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});
client.delete({
index: 'allevents',
type: '_doc',
id:"2"
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});

当我通过传递单个 id 值删除单个文档时。它工作正常。

但我想在单个查询中删除多个文档。我们该怎么办?

我试过了

 client.delete({
index: 'allevents',
type: '_doc',
id: ["2","3"]
})

此函数返回错误。

最佳答案

我建议利用bulk method/API ,像这样:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});

var idsToDelete = ["2", "3"];
var bulk = idsToDelete.map(id => {
return {
'delete': {
'_index': 'allevents',
'_type': '_doc',
'_id': id
}
};
});

client.bulk({
body: bulk
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});

另一种方法是利用 deleteByQuery method/API :

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});

var idsToDelete = ["2", "3"];

client.deleteByQuery({
index: 'allevents',
type: '_doc',
body: {
query: {
terms: {
_id: idsToDelete
}
}
}
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});

关于javascript - 如何删除elasticsearch索引中的多个id文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com