Elasticsearch Elasticsearch Clone Index: A Comprehensive Guide

By Opster Team

Updated: Jul 23, 2023

| 3 min read

Introduction

Cloning an Elasticsearch index is a useful technique for creating a copy of an existing index with the same settings, mappings, and aliases. This can be helpful in various scenarios, such as testing changes to an index, creating a backup, or migrating data to a new cluster. In this article, we will discuss the process of cloning an Elasticsearch index, its benefits, and some best practices to follow. If you want to learn about Elasticsearch index – how to create, list, query and delete indices, check out this guide.

Benefits of Cloning an Elasticsearch Index

  1. Testing and Development: Cloning an index allows you to create a separate environment for testing and development without affecting the production index. This enables you to experiment with different configurations, mappings, and settings without impacting the live data.
  1. Backup and Recovery: Creating a clone of an index can serve as a backup in case of data loss or corruption. In the event of a disaster, you can quickly restore the data from the cloned index.
  1. Data Migration: Cloning an index can be useful when migrating data to a new Elasticsearch cluster or upgrading to a newer version. By cloning the index, you can ensure that the new environment has the same settings and mappings as the original index.
  1. Index Maintenance: Cloning an index can help with index maintenance tasks, such as reindexing or shrinking. By creating a clone, you can perform these operations on the cloned index without affecting the original index.

Step-by-Step Guide to Clone an Elasticsearch Index

Step 1: Check the Index Health

Before cloning an index, ensure that the index is in a healthy state. You can check the index health using the following command:

GET /_cluster/health/<source_index>

The response must indicate the index status as “green”. Avoid cloning an index with a “red” status, as it may have missing or corrupted data.

Step 2: Make the Source Index Read-Only

Before an index can be cloned, it must be made read-only with the command below:

PUT /<source_index>/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

Replace `<source_index>` with the name of the index you want to clone and that needs to be made read-only. This means that only indexes that are not being actively written to can be cloned, hence it might not be applicable to your use case if it’s not possible to stop the indexing process.

Step 3: Create a Clone of the Index

To clone an index, use the `_clone` API with the following command:

POST /<source_index>/_clone/<target_index>

Replace `<source_index>` with the name of the index you want to clone and `<target_index>` with the name of the new index. For example:

POST /my_source_index/_clone/my_target_index

This command will create a new index named `my_target_index` with the same settings, mappings, and aliases as `my_source_index`.

Step 4: Monitor the Cloning Process

You can monitor the progress of the cloning process using the `_cat/recovery` API or the cluster health API like in the step 1 above.

GET /_tasks?detailed=true&actions=*reindex

Look for the task with the “description” field containing the source and target index names. The “status” field will show the progress of the cloning process.

Step 5: Verify the Cloned Index

Once the cloning process is complete, verify that the new index has the same settings, mappings, and aliases as the original index. Use the following commands to compare the source and target indices:

GET /<source_index>/_settings
GET /<target_index>/_settings

GET /<source_index>/_mapping
GET /<target_index>/_mapping

GET /<source_index>/_alias
GET /<target_index>/_alias

If the responses are identical, the cloning process was successful.

Step 6: Make the Source Index Read-Write Again

Ater cloning your source index, you can make it read-write again with the command below:

PUT /<source_index>/_settings
{
  "settings": {
    "index.blocks.write": null
  }
}

Replace `<source_index>` with the name of the index you have just cloned.

Best Practices for Cloning an Elasticsearch Index

  1. Clone only healthy indices: Ensure that the index you want to clone is in a healthy state before initiating the cloning process.
  2. Original index state: The original index must be in a read-only state before it can be cloned. This is to ensure data consistency during the cloning process.
  1. Monitor the cloning process: Keep an eye on the progress of the cloning process using the `_cat/recovery` API to ensure it completes successfully.
  1. Verify the cloned index: After cloning, compare the settings, mappings, and aliases of the source and target indices to ensure they are identical.
  2. Plan for additional resources: Cloning an index may require additional resources, such as disk space and memory. Ensure that your Elasticsearch cluster has sufficient resources to handle the cloned index.

Conclusion

In conclusion, cloning an Elasticsearch index is a valuable technique for various use cases, including testing, backup, data migration, and index maintenance. By following the step-by-step guide and best practices outlined in this article, you can efficiently and effectively clone an Elasticsearch index while minimizing the impact on your production environment.