如何在Elasticsearch索引中删除多个id文档
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 / bulk
:
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:
deleteByQuery