Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Indexes by index name and type using elasticSearch 2.3.3 in java

I have a project in java where I index the data using elastic search 2.3.3. The indexes are of two types.

My index doc looks like:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
  "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
        {
           "_index": "test_index",
           "_type": "movies",
           "_id": "uReb0g9KSLKS18sTATdr3A",
           "_score": 1,
           "_source": {
              "genre": "Thriller"
            }
       },
       {
           "_index": "test_index",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sTATdr3B",
           "_score": 1,
           "_source": {
              "genre": "SuperNatural"
            }
        },
        {
           "_index": "index1",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sT76ng3B",
           "_score": 1,
           "_source": {
              "genre": "Romance"
            }
         }
      ]
   }
}

I need to delete index of a particular name and type only.

For eg:- From the above doc, I want to delete indexes with Name "test_index" and type "drama".

So the result should look like:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
  "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
        {
           "_index": "test_index",
           "_type": "movies",
           "_id": "uReb0g9KSLKS18sTATdr3A",
           "_score": 1,
           "_source": {
              "genre": "Thriller"
            }
       },
       {
           "_index": "index1",
           "_type": "drama",
           "_id": "cReb0g9KSKLS18sT76ng3B",
           "_score": 1,
           "_source": {
              "genre": "Romance"
            }
         }
      ]
   }
}

Solutions tried:

client.admin().indices().delete(new DeleteIndexRequest("test_index").actionGet();

But it delete both indexes with name "test_index"

I have also tried various queries in sense beta plugin like:

DELETE /test_index/drama

It gives the error: No handler found for uri [/test_index/drama] and method [DELETE]

DELETE /test_index/drama/_query?q=_id:*&analyze_wildcard=true

It also doesn't work.

When I fire delete index request at that time id of indexes are unknown to us and I have to delete the indexes by name and type only.

How can I delete the required indexes using java api?

like image 635
ajain Avatar asked Jul 27 '16 06:07

ajain


1 Answers

This used to be possible till ES 2.0 using the delete mapping API, however since 2.0 Delete Mapping API does not exist any more. To do this you will have to install the Delete by Query plugin. Then you can simply do a match all query on your index and type and then delete all of them.

The query will look something like this:

DELETE /test_index/drama/_query
{
  "query": { 
    "query": {
        "match_all": {}
    }
  }
}

Also keep in mind that this will delete the documents in the mapping and not the mapping itself. If you want to remove the mapping too you'll have to reindex without the mapping.

This might be able to help you with the java implementation

like image 148
adityasinghraghav Avatar answered Oct 31 '22 18:10

adityasinghraghav