0
0
ElasticsearchHow-ToBeginner · 3 min read

How to Reindex in Elasticsearch: Syntax and Examples

To reindex in Elasticsearch, use the _reindex API which copies documents from a source index to a destination index. You send a POST request to /_reindex with JSON specifying the source and destination indexes.
📐

Syntax

The _reindex API requires a POST request to /_reindex with a JSON body. The JSON must include a source object specifying the source index and a dest object specifying the destination index.

Optionally, you can add a query inside source to filter documents, or use script to modify documents during reindexing.

json
{
  "source": {
    "index": "source_index"
  },
  "dest": {
    "index": "destination_index"
  }
}
💻

Example

This example copies all documents from old_index to new_index. It demonstrates the basic reindex operation without filters or scripts.

json
POST /_reindex
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  }
}
Output
{ "took": 123, "timed_out": false, "total": 1000, "updated": 0, "created": 1000, "deleted": 0, "batches": 1, "version_conflicts": 0, "noops": 0, "retries": { "bulk": 0, "search": 0 }, "throttled_millis": 0, "requests_per_second": -1.0, "throttled_until_millis": 0, "failures": [] }
⚠️

Common Pitfalls

  • Forgetting to create the destination index before reindexing can cause errors.
  • Not handling version conflicts if documents already exist in the destination index.
  • Using _reindex without filters may copy unwanted data.
  • Running reindex on large datasets without throttling can overload the cluster.

Always test reindexing on a small dataset first.

json
POST /_reindex
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  }
}

-- Wrong: Destination index missing

PUT /new_index
{
  "settings": {
    "number_of_shards": 1
  }
}

-- Right: Create destination index before reindexing
📊

Quick Reference

Remember these key points when reindexing:

  • Use POST /_reindex with source and dest indexes.
  • Create the destination index before reindexing.
  • Use query to filter documents if needed.
  • Use script to modify documents during reindex.
  • Monitor the output for errors and version conflicts.

Key Takeaways

Use the _reindex API with source and destination indexes to copy documents.
Always create the destination index before starting reindexing.
Filter documents with a query to avoid copying unwanted data.
Use scripts to modify documents during reindex if needed.
Check the reindex response for errors and version conflicts.