Reindexing data helps you copy or move data from one place to another inside Elasticsearch. It is useful when you want to change the structure or fix mistakes without losing data.
Reindexing data in Elasticsearch
POST _reindex
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
}
}The source section tells Elasticsearch where to copy data from.
The dest section tells where to copy data to.
products_v1 to products_v2.POST _reindex
{
"source": { "index": "products_v1" },
"dest": { "index": "products_v2" }
}status equal to error from logs to error_logs.POST _reindex
{
"source": {
"index": "logs",
"query": { "term": { "status": "error" } }
},
"dest": { "index": "error_logs" }
}POST _reindex
{
"source": { "index": "old_index" },
"dest": { "index": "new_index" },
"script": {
"source": "ctx._source['new_field'] = 'default_value'"
}
}This command copies all documents from the library_books index to a new index called library_books_backup. It is like making a backup copy of your data.
POST _reindex
{
"source": {
"index": "library_books"
},
"dest": {
"index": "library_books_backup"
}
}Reindexing can take time depending on how much data you have.
You can use a query to copy only some documents.
Use script to change data while copying.
Reindexing copies data from one index to another.
It helps change data structure or fix issues without losing data.
You can filter or modify data during reindexing.
Practice
What is the main purpose of _reindex API in Elasticsearch?
Solution
Step 1: Understand the function of
The_reindex_reindexAPI is designed to copy documents from a source index to a destination index.Step 2: Differentiate from other operations
Deleting an index, updating cluster settings, or creating new indexes are different APIs and not the purpose of_reindex.Final Answer:
To copy data from one index to another -> Option BQuick Check:
_reindexcopies data [OK]
- Confusing reindex with deleting indexes
- Thinking reindex updates cluster settings
- Assuming reindex creates new indexes without data
Which of the following is the correct minimal syntax to reindex data from old_index to new_index using Elasticsearch's REST API?
{
"source": {"index": "old_index"},
"dest": {"index": "new_index"}
}Solution
Step 1: Check the required keys for reindex
The correct keys aresourceanddestwith nestedindexnames.Step 2: Validate the index names and key spelling
{ "source": { "index": "old_index" }, "dest": { "index": "new_index" } } uses correct keys and index names. { "source": { "index": "new_index" }, "dest": { "index": "old_index" } } swaps source and destination incorrectly. { "source": { "index": "old_index" }, "destination": { "index": "new_index" } } uses wrong keydestination. { "src": { "index": "old_index" }, "dest": { "index": "new_index" } } uses wrong keysrc.Final Answer:
{ "source": { "index": "old_index" }, "dest": { "index": "new_index" } } -> Option AQuick Check:
Usesourceanddestkeys exactly [OK]
- Swapping source and destination indexes
- Using wrong keys like 'src' or 'destination'
- Misspelling 'dest' or 'source'
Given the following reindex request, what will be the result?
{
"source": { "index": "products" },
"dest": { "index": "products_new" },
"script": {
"source": "ctx._source.price = ctx._source.price * 1.1"
}
}Assuming products has documents with a price field.
Solution
Step 1: Understand the script in reindex
The script modifies each document'spriceby multiplying it by 1.1 during reindexing.Step 2: Effect on documents
All documents fromproductsare copied toproducts_newwith updatedpricevalues increased by 10%.Final Answer:
All documents copied with price increased by 10% -> Option DQuick Check:
Script modifies price during reindex [OK]
- Assuming script filters documents instead of modifying
- Thinking script causes syntax error
- Believing price remains unchanged
Identify the error in this reindex request:
{
"source": { "index": "old_index" },
"dest": { "index": "new_index" },
"query": {
"match": { "status": "active" }
}
}Solution
Step 1: Check placement of query in reindex
Thequerymust be inside thesourceobject to filter documents.Step 2: Validate other parts
Thematchsyntax is correct, anddestindex name is valid.Final Answer:
The query should be inside the "source" object -> Option AQuick Check:
Put query inside source to filter during reindex [OK]
- Placing query outside source object
- Misunderstanding match query syntax
- Thinking destination index name is invalid
You want to reindex data from logs_2023 to logs_archive but only include documents where level is "error" and add a new field archived set to true. Which reindex request achieves this?
Solution
Step 1: Filter documents by level "error" inside source
Thequerywithtermfilter must be inside thesourceobject to select only error level logs.Step 2: Add new field "archived" with value true via script
Thescriptsetsctx._source.archived = trueto add the new field during reindex.Step 3: Validate requests
The request that placesqueryinsidesourcewithtermfilter forlevel: "error" andscriptsettingctx._source.archived = trueis correct. Placingqueryat top level is invalid. Usingmatchinstead oftermor settingarchivedtofalseis incorrect.Final Answer:
{ "source": { "index": "logs_2023", "query": { "term": { "level": "error" } } }, "dest": { "index": "logs_archive" }, "script": { "source": "ctx._source.archived = true" } } -> Option CQuick Check:
Filter in source + script sets field true [OK]
- Placing query outside source object
- Setting wrong value for new field
- Using match instead of term for exact filter
Ready to prove it?
Pass the test and this pattern moves unknown
