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

Time:06-18

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related