Reindex documents Generally available; Added in 2.3.0
Copy documents from a source to a destination. You can copy all documents to the destination index or reindex a subset of the documents. The source can be any existing index, alias, or data stream. The destination must differ from the source. For example, you cannot reindex a data stream into itself.
IMPORTANT: Reindex requires _source
to be enabled for all documents in the source. The destination should be configured as wanted before calling the reindex API. Reindex does not copy the settings from the source or its associated template. Mappings, shard counts, and replicas, for example, must be configured ahead of time.
If the Elasticsearch security features are enabled, you must have the following security privileges:
- The
read
index privilege for the source data stream, index, or alias. - The
write
index privilege for the destination data stream, index, or index alias. - To automatically create a data stream or index with a reindex API request, you must have the
auto_configure
,create_index
, ormanage
index privilege for the destination data stream, index, or alias. - If reindexing from a remote cluster, the
source.remote.user
must have themonitor
cluster privilege and theread
index privilege for the source data stream, index, or alias.
If reindexing from a remote cluster, you must explicitly allow the remote host in the reindex.remote.whitelist
setting. Automatic data stream creation requires a matching index template with data stream enabled.
The dest
element can be configured like the index API to control optimistic concurrency control. Omitting version_type
or setting it to internal
causes Elasticsearch to blindly dump documents into the destination, overwriting any that happen to have the same ID.
Setting version_type
to external
causes Elasticsearch to preserve the version
from the source, create any documents that are missing, and update any documents that have an older version in the destination than they do in the source.
Setting op_type
to create
causes the reindex API to create only missing documents in the destination. All existing documents will cause a version conflict.
IMPORTANT: Because data streams are append-only, any reindex request to a destination data stream must have an op_type
of create
. A reindex can only add new documents to a destination data stream. It cannot update existing documents in a destination data stream.
By default, version conflicts abort the reindex process. To continue reindexing if there are conflicts, set the conflicts
request body property to proceed
. In this case, the response includes a count of the version conflicts that were encountered. Note that the handling of other error types is unaffected by the conflicts
property. Additionally, if you opt to count version conflicts, the operation could attempt to reindex more documents from the source than max_docs
until it has successfully indexed max_docs
documents into the target or it has gone through every document in the source query.
Refer to the linked documentation for examples of how to reindex documents.
Required authorization
- Index privileges:
read
,write
Query parameters
-
If
true
, the request refreshes affected shards to make this operation visible to search. -
The throttle for this request in sub-requests per second. By default, there is no throttle.
-
The period of time that a consistent view of the index should be maintained for scrolled search.
Values are
-1
or0
. -
The number of slices this task should be divided into. It defaults to one slice, which means the task isn't sliced into subtasks.
Reindex supports sliced scroll to parallelize the reindexing process. This parallelization can improve efficiency and provide a convenient way to break the request down into smaller parts.
NOTE: Reindexing from remote clusters does not support manual or automatic slicing.
If set to
auto
, Elasticsearch chooses the number of slices to use. This setting will use one slice per shard, up to a certain limit. If there are multiple sources, it will choose the number of slices based on the index or backing index with the smallest number of shards.Value is
auto
. -
The period each indexing waits for automatic index creation, dynamic mapping updates, and waiting for active shards. By default, Elasticsearch waits for at least one minute before failing. The actual wait time could be longer, particularly when multiple waits occur.
Values are
-1
or0
. -
The number of shard copies that must be active before proceeding with the operation. Set it to
all
or any positive integer up to the total number of shards in the index (number_of_replicas+1
). The default value is one, which means it waits for each primary shard to be active.Values are
all
orindex-setting
. -
If
true
, the request blocks until the operation is complete. -
If
true
, the destination must be an index alias.
Body Required
-
Values are
abort
orproceed
. -
The maximum number of documents to reindex. By default, all documents are reindexed. If it is a value less then or equal to
scroll_size
, a scroll will not be used to retrieve the results for the operation.If
conflicts
is set toproceed
, the reindex operation could attempt to reindex more documents from the source thanmax_docs
until it has successfully indexedmax_docs
documents into the target or it has gone through every document in the source query.
POST _reindex { "source": { "index": ["my-index-000001", "my-index-000002"] }, "dest": { "index": "my-new-index-000002" } }
resp = client.reindex( source={ "index": [ "my-index-000001", "my-index-000002" ] }, dest={ "index": "my-new-index-000002" }, )
const response = await client.reindex({ source: { index: ["my-index-000001", "my-index-000002"], }, dest: { index: "my-new-index-000002", }, });
response = client.reindex( body: { "source": { "index": [ "my-index-000001", "my-index-000002" ] }, "dest": { "index": "my-new-index-000002" } } )
$resp = $client->reindex([ "body" => [ "source" => [ "index" => array( "my-index-000001", "my-index-000002", ), ], "dest" => [ "index" => "my-new-index-000002", ], ], ]);
curl -X POST -H "Authorization: ApiKey $ELASTIC_API_KEY" -H "Content-Type: application/json" -d '{"source":{"index":["my-index-000001","my-index-000002"]},"dest":{"index":"my-new-index-000002"}}' "$ELASTICSEARCH_URL/_reindex"
client.reindex(r -> r .dest(d -> d .index("my-new-index-000002") ) .source(s -> s .index(List.of("my-index-000001","my-index-000002")) ) );
{ "source": { "index": ["my-index-000001", "my-index-000002"] }, "dest": { "index": "my-new-index-000002" } }
{ "source": { "index": "metricbeat-*" }, "dest": { "index": "metricbeat" }, "script": { "lang": "painless", "source": "ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'" } }
{ "max_docs": 10, "source": { "index": "my-index-000001", "query": { "function_score" : { "random_score" : {}, "min_score" : 0.9 } } }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-new-index-000001", "version_type": "external" }, "script": { "source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')}", "lang": "painless" } }
{ "source": { "remote": { "host": "http://otherhost:9200", "username": "user", "password": "pass" }, "index": "my-index-000001", "query": { "match": { "test": "data" } } }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "my-index-000001", "slice": { "id": 0, "max": 2 } }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "source", "query": { "match": { "company": "cat" } } }, "dest": { "index": "dest", "routing": "=cat" } }
{ "source": { "index": "source" }, "dest": { "index": "dest", "pipeline": "some_ingest_pipeline" } }
{ "source": { "index": "my-index-000001", "query": { "term": { "user.id": "kimchy" } } }, "dest": { "index": "my-new-index-000001" } }
{ "max_docs": 1, "source": { "index": "my-index-000001" }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "my-index-000001", "_source": ["user.id", "_doc"] }, "dest": { "index": "my-new-index-000001" } }
{ "source": { "index": "my-index-000001" }, "dest": { "index": "my-new-index-000001" }, "script": { "source": "ctx._source.tag = ctx._source.remove(\"flag\")" } }