繁体   English   中英

elasticsearch中如何一次性删除多个属于不同索引的文档?

[英]How can we delete multiple documents at once in elastic search that belongs to different indexes?

我知道我们有delete_by_query API 可以完成这项工作,但我正在寻找使用批量 API 的解决方案。我试图遵循https://www.elastic.co/guide/en/elasticsearch/reference/current/docs- bulk.html但它仅在我们从一个索引中删除文档时才起作用。 如果我们有多个以逗号 (,) 分隔的索引,例如index1,index2 ,那该怎么办

您可以像这样构建请求:

PUT _bulk
{ "delete" : { "_index" : "products", "_id" : 1 } }
{ "delete" : { "_index" : "idx_movies", "_id" : 1 } }

在不同的索引中删除相同的_id。

响应将是(如果找到文档,将被删除,否则将找不到响应:

{
  "took" : 6,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "_index" : "products",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 7,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "idx_movies",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1012,
        "_primary_term" : 2,
        "status" : 404
      }
    }
  ]
}

您可以使用具有不同索引名称的Bulk API ,如下所示:

POST _bulk
{ "delete" : { "_index" : "index1", "_id" : "1" } }
{ "delete" : { "_index" : "index1", "_id" : "2" } }
{ "delete" : { "_index" : "index2", "_id" : "3" } }
{ "delete" : { "_index" : "index3", "_id" : "4" } }

类似的,您也可以使用 Elastic Java 或任何其他语言客户端来实现。

暂无
暂无

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

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