liveBook · Manning

Index shrinking

As explained in Elasticsearch in Action, Second Edition

Index shrinking is the Elasticsearch operation that creates a new index with fewer shards than a source index while preserving its documents.

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

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains preparation, allocation, shard-count constraints, target settings, and segment linking.
Examples
Uses an all_cars index distributed across 50 shards and shrunk to one shard through REST APIs.
What it assumes
Assumes familiarity with index settings, shards, replicas, and node allocation; teaches the shrink preparation sequence.
What you can do after
Operate shrinking through REST settings and the _shrink endpoint after preparing shard allocation.
How it connects
Connects index shrinking with index splitting, shard allocation, replicas, and index lifecycle management.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.