How it works...

The shrink API reduces the number of shards by executing the following steps:

  1. Elasticsearch creates a new target index with the same definition as the source index, but with a smaller number of primary shards.
  2. Elasticsearch hard-links (or copies) segments from the source index into the target index.
If the filesystem doesn't support hard-linking, then all segments are copied into the new index, which is a much more time-consuming process. Elasticsearch recovers the target index as though it were a closed index that has just been reopened. On a Linux system, the process is very fast due to hard links.

The prerequisites for executing a shrink are as follows:

  • All the primary shards must be on the same node
  • The target index must not exist
  • The target number of shards must be a factor of the number of shards in the source index