最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在Elasticsearch索引中删除多个id文档

运维笔记admin10浏览0评论

如何在Elasticsearch索引中删除多个id文档

如何在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
发布评论

评论列表(0)

  1. 暂无评论