Shrink Shards Of a Index in Elasticsearch

To shrink a shard in elasticsearch we already have a “/old_index/_shrink/new_index” endpoint , which creates a new index with less shards. An example of endpoint request :

# Tested Using Elasticsearch 6.5#for kibana
POST old_index/_shrink/new_index?copy_settings=true
{
“settings”: {
“index.routing.allocation.require._name”: null,
“index.blocks.write”: null,
“index.number_of_replicas”: 1,
“index.number_of_shards”: 1,
“index.codec”: “best_compression”
},
“aliases”: {
“my_search_indices”: {}
}
}
#curl
curl -XPOST -H "Content-type:application/json" localhost:9200/old_index/_shrink/new_index?copy_settings=true -d '{"settings": {"index.blocks.write": null,"index.number_of_replicas": 1,"index.number_of_shards": 1, "index.codec": "best_compression" },"aliases": {"my_search_indices": {}}}'

after this execution you would have two indexes with same number of documents and mappings but with different shards and replicas.

Sometimes problem occurs like:

  • Read only index required or should be not writable
  • The index must be read-only.
  • FORBIDDEN/12/index read-only / allow delete

Then first run the below request:

#for kibana
PUT old_index/_settings
{
“settings”: {
“index.blocks.write”: false,
“index.blocks.read_only_allow_delete”: null
}
}
#curl
curl -XPUT -H "Content-type:application/json" localhost:9200/old_index/_settings -d '{"settings": {"index.blocks.write": true,"index.blocks.read_only_allow_delete": null }}'

Verify the documents in new_index , and when you are sure its ok then you can delete the old index by requesting:

#kibana
DELETE old_index
#curl
curl -XDELETE localhost:9200/old_index

Github: https://github.com/bazingarj/elasticsearch-shrink-index-shards/tree/master

Written by

#DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store