繁体   English   中英

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

[英]How to delete multiple id documents in elasticsearch index

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"]
})

此 function 返回错误。

我建议利用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方法/ 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);
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM