Elasticsearch Resetting Index in Elasticsearch: A Comprehensive Guide

By Opster Team

Updated: Jul 23, 2023

| 2 min read

Introduction

In Elasticsearch, an index is a collection of documents that share similar characteristics and are stored together for efficient querying. There are times when you may need to reset an index, such as when you want to reindex data, change the index settings, or delete and recreate an index.

In this article, we will discuss the process of resetting an index in Elasticsearch, including the steps to delete, create, and reindex data. If you want to learn about Elasticsearch red status, check out this guide.

Step 1: Create a New Index with Updated Settings

Once the existing index has been deleted, you can create a new index with the desired settings. To do this, you can use the PUT index API. The following command will create a new index named “my_index” with the specified settings:

PUT /my_index
{
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "properties": {
      "field1": {
        "type": "text"
      },
      "field2": {
        "type": "keyword"
      }
    }
  }
}

In this example, we have specified the number of shards and replicas for the new index, as well as the mappings for the fields. Make sure to replace “my_index” with the name of the index you want to create, and update the settings and mappings as needed.

Step 2: Reindex Data from the Old Index to the New Index

After creating the new index, you need to reindex the data from the old index to the new one. To do this, you can use the Reindex API. The following command will reindex data from the old index named “old_index” to the new index named “my_index”:

POST /_reindex?wait_for_completion=false
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "my_index"
  }
}

Make sure to replace “old_index” with the name of the old index, and “my_index” with the name of the new index. After executing this command with `wait_for_completion=false`, you should receive a response containing a task ID indicating that the reindexing process has started.

Note that the Reindex API performs the reindexing process in the background, so you may need to monitor the progress of the reindexing task. You can do this by using the Task Management API. The following command will retrieve the status of a reindexing task with the specified task ID:

GET /_tasks/{task_id}

Make sure to replace “{task_id}” with the actual task ID returned in the response when you started the reindexing process.

Step 3: Verify the New Index

Once the reindexing process is complete, you can verify that the new index has been created successfully and contains the expected data. To do this, you can use the Search API. The following command will search for documents in the new index named “my_index”:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  }
}

Make sure to replace “my_index” with the name of the new index. After executing this command, you should receive a response containing the documents from the new index.

Step 4: Delete the Old Index

After verifying that your new index has been created successfully, you can delete the old one. To do this, you can use the DELETE index API. The following command will delete the index named old_index”:

DELETE /old_index

Make sure to replace “old_index” with the name of the index you want to delete. After executing this command, you should receive a response indicating that the index has been deleted successfully.

Conclusion

In this article, we have discussed the process of resetting an index in Elasticsearch, including the steps to create, reindex data, and delete the old index. By following these steps, you can reset an index and update its settings and mappings as needed. Remember to always backup your data before performing any index operations, as deleting an index will permanently remove its data from Elasticsearch.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?