简体   繁体   中英

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

I know we have delete_by_query API that can do this job, but I'm looking for a solution using bulk API. I tried to follow https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html but it's only working when we delete documents from one index. what about when we have more than one index that having comma(,) separated indexes like index1,index2

You can build the request like that:

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

Delete in different indice the same _id.

The response will be (If find the doc, will be deleted else the response will be not found:

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

You can use Bulk API with different index name as shown below:

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

Similar you can achive using Elastic Java or any other language client as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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