Index shrinking creates a new Elasticsearch index with fewer shards while preserving the source documents. It consolidates data that is spread across many shards, which can suit quieter periods after a demand spike or when extra data nodes are no longer needed. section 6.8.2
Prepare the source index before shrinking:
- Make the source index read-only by setting index.blocks.write to true. section 6.8.2
- Allocate all source shards to one node by setting index.routing.allocation.require._name. section 6.8.2
- Choose a target shard count no larger than the source count and make it a factor of that count. section 6.8.2
Listing 6.43 Prerequisites before shrinking the index
PUT all_cars/_settings
{
"settings": {
"index.blocks.write": true,
"index.routing.allocation.require._name": "node1"
}
}
Apply the write block and single-node allocation required before the shrink operation. section 6.8.2
Shrink all_cars from 50 shards to 1
After preparing all_cars, call PUT all_cars/_shrink/all_cars_new. Set index.number_of_shards to 1 and choose the replica count for the new index. Nullify index.blocks.write and index.routing.allocation.require._name so those source restrictions are not carried into the target. The operation creates hard links to the source index’s file segments. section 6.8.2
Listing 6.44 Shrinking the index
PUT all_cars/_shrink/all_cars_new #1
{
"settings":{
"index.blocks.write":null, #2
"index.routing.allocation.require._name":null, #3
"index.number_of_shards":1, #4
"index.number_of_replicas":5
}
}
Inspect the focused shrink request, including target shard and replica settings and cleared source restrictions. section 6.8.2
