Shrink Shards Of a Index in Elasticsearch
1 min readJul 24, 2020
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