elasticsearch-reindex
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElasticsearch Reindex
Elasticsearch Reindex
Copy documents from source indices or data streams to a destination using . An expert reindex workflow
prepares the destination explicitly, chooses local versus remote execution, filters at the source when only a subset is
needed, runs long copies asynchronously, tracks the task to completion, and verifies the destination document count
before reporting results.
<!-- begin-partial: preamble -->POST /_reindex使用将文档从源索引或数据流复制到目标索引。专业的重新索引工作流会明确准备目标索引,选择本地或远程执行方式,仅在需要子集时在源端过滤,异步运行长时复制任务,跟踪任务直至完成,并在报告结果前验证目标文档数量。
<!-- begin-partial: preamble -->POST /_reindexEnvironment Configuration
环境配置
This skill executes Elasticsearch operations through the CLI. If the
CLI is not installed, tell the user what it is needed for. Do
not guess credentials, call the HTTP API directly, or attempt other workarounds.
elasticelasticThis skill references operations in HTTP-shorthand form (e.g., , , ,
, ). The Operations table at the end of this document
maps each shorthand to the equivalent CLI command — always use the CLI rather than calling the HTTP API
directly.
<!-- end-partial: preamble -->GET /GET /_cat/indicesGET /{index}/_mappingGET /{index}/_settings/index.modePOST /_queryelastic本技能通过 CLI执行Elasticsearch操作。如果未安装 CLI,请告知用户其用途。请勿猜测凭证、直接调用HTTP API或尝试其他变通方法。
elasticelastic本技能使用HTTP简写形式引用操作(例如、、、、)。本文档末尾的操作表格将每个简写映射为对应的 CLI命令——请始终使用CLI而非直接调用HTTP API。
<!-- end-partial: preamble -->GET /GET /_cat/indicesGET /{index}/_mappingGET /{index}/_settings/index.modePOST /_queryelasticProcess
操作流程
-
Confirm connectivity and deployment type. Call. Read
GET /andbuild_flavorto know whether shard, replica, and cluster-settings APIs are available (Serverless manages shards/replicas internally and blocks mostversion.numberAPIs). The decision: continue only when the cluster is reachable. If the call fails, stop — do not guess endpoints or credentials._cluster/* -
Decide local versus remote reindex. Compare where the source and destination live.
- Same cluster — use local reindex: and
source.indexonly. Do not adddest.indexwhen both indices are on the cluster you are connected to.source.remote - Different cluster — use reindex from remote: add with the remote cluster URL and credentials. Remote reindex does not support slicing; compensate with query-based partitioning (date ranges, term filters) across parallel requests. Confirm the remote host is allowlisted on Self-Managed / ECH (
source.remotein cluster config); Serverless manages allowlisting internally (ECH remotes only, Tech Preview).reindex.remote.whitelist
Data needed: source index name(s), destination index name, and whether they share a cluster. - Same cluster — use local reindex:
-
Inspect the source — never guess field names or counts. Callto ground field names and types. Call
GET /{source}/_mapping(orGET /{source}/_counton Self-Managed / ECH) to learn how many documents exist.GET /_cat/count/{source}?h=countThe decision: full copy versus filtered subset.- Full copy — omit (match-all behavior).
source.query - Filtered subset — add with Query DSL. For time ranges, use a
source.queryfilter on the timestamp field (commonlyrange), e.g.@timestampfor January 2025. Do not run a full-index copy when the user asked for a date range or other filter."gte": "2025-01-01", "lt": "2025-02-01"
Data needed: the user's filter criteria and the mapping-confirmed field names. - Full copy — omit
-
Prepare the destination index before copying.does not copy mappings, shard counts, or analyzers. Create the destination with explicit settings and mappings derived from the source mapping via
_reindex.PUT /{dest}- On Self-Managed / ECH: set and
number_of_replicas: 0on the destination during the copy for write throughput; restore production values afterward withrefresh_interval: "-1".PUT /{dest}/_settings - On Serverless: omit and
number_of_shards(managed by Elastic); you may setnumber_of_replicasduring the copy.refresh_interval: "-1" - For data stream destinations: ensure an index template with exists, create the data stream, and set
data_stream: {}todest.op_type(append-only)."create"
The decision: create/prepare the target rather than relying on auto-creation with dynamic mapping. Wrong or missing mappings cause partial failures or silent type coercion.Data needed: destination name, corrected or compatible mappings, and deployment-specific settings constraints. - On Self-Managed / ECH: set
-
Build and submit the reindex request. Callfor any copy that may take more than a few seconds or when the user says the index is large — the response returns a task id immediately instead of blocking.
POST /_reindex?wait_for_completion=falseRequest body essentials:- — source index or data stream (correct name, not reversed with
source.index).dest.index - — prepared destination from step 4.
dest.index - — include only when step 3 chose a filtered subset.
source.query - — when retrying a partially complete reindex.
conflicts: "proceed" - Optional tuning: (batch size),
source.size(throttle),requests_per_secondon local reindex only (parallelize per primary shard — never for remote),slices=auto(increase keep-alive on slow clusters),scroll(test runs),max_docs(transform),script(ingest enrichment).dest.pipeline
Example filtered subset (January 2025 only):json{ "source": { "index": "eval-reindex-src", "query": { "range": { "@timestamp": { "gte": "2025-01-01", "lt": "2025-02-01" } } } }, "dest": { "index": "eval-reindex-jan" } }Do not reach for,_split, or snapshot/restore when the task is a filtered subset copy or a straight document migration — those APIs solve different problems._shrink -
Track the task to completion. Store the task id from the reindex response. Polluntil
GET /_tasks/{task_id}iscompleted. Readtrue,status.total, andstatus.created. On Self-Managed / ECH you may also list active reindex tasks withresponse.failures; on Serverless, query by task id only (list/cancel are not available). Adjust throttling mid-flight withGET /_tasks?actions=*reindex&detailedwithout canceling.POST /_reindex/{task_id}/_rethrottle?requests_per_second=N -
Verify and report the destination count. Call(works on all deployment types). On Self-Managed / ECH you may also use
GET /{dest}/_count. Compare source filter expectations to the destination count. Report the exact count from the destination — do not estimate or guess.GET /_cat/count/{dest}?h=countAfter a successful full copy, restore production settings on the destination with(replicas and refresh interval on Self-Managed / ECH; refresh interval only on Serverless).PUT /{dest}/_settings
-
确认连通性与部署类型。调用,读取
GET /和build_flavor,以了解分片、副本和集群设置API是否可用(Serverless会在内部管理分片/副本,并阻止大多数version.numberAPI)。决策:仅当集群可访问时继续操作。如果调用失败,请停止——请勿猜测端点或凭证。_cluster/* -
选择本地或远程重新索引。比较源索引和目标索引的所在位置。
- 同一集群 — 使用本地重新索引:仅需指定和
source.index。当两个索引都在当前连接的集群上时,请勿添加dest.index参数。source.remote - 不同集群 — 使用远程重新索引:添加参数,包含远程集群的URL和凭证。远程重新索引不支持分片处理;可通过基于查询的分区(日期范围、术语过滤器)并行发送请求来弥补。确认远程主机已在自托管/ECH的集群配置中加入白名单(
source.remote);Serverless会在内部管理白名单(仅支持ECH远程,技术预览阶段)。reindex.remote.whitelist
所需信息:源索引名称、目标索引名称,以及两者是否处于同一集群。 - 同一集群 — 使用本地重新索引:仅需指定
-
检查源索引——切勿猜测字段名或文档数量。调用确认字段名和类型。调用
GET /{source}/_mapping(在自托管/ECH上可使用GET /{source}/_count)了解文档总数。GET /_cat/count/{source}?h=count决策:完整复制还是过滤子集。- 完整复制 — 省略(默认匹配所有文档)。
source.query - 过滤子集 — 添加参数并使用Query DSL。对于时间范围,可对时间戳字段(通常为
source.query)使用@timestamp过滤器,例如range表示2025年1月的数据。当用户要求按日期范围或其他条件过滤时,请勿执行全索引复制。"gte": "2025-01-01", "lt": "2025-02-01"
所需信息:用户的过滤条件,以及经映射确认的字段名。 - 完整复制 — 省略
-
复制前准备目标索引。不会复制映射、分片数量或分析器。需通过
_reindex创建目标索引,并基于源索引映射设置明确的配置和映射。PUT /{dest}- 在自托管/ECH上:复制期间将目标索引的设为
number_of_replicas,0设为refresh_interval以提升写入吞吐量;复制完成后,通过"-1"恢复生产环境配置。PUT /{dest}/_settings - 在Serverless上:无需设置和
number_of_shards(由Elastic管理);复制期间可设置number_of_replicas。refresh_interval: "-1" - 对于数据流目标:确保存在包含的索引模板,创建数据流,并将
data_stream: {}设为dest.op_type(仅追加模式)。"create"
决策:手动创建/准备目标索引,而非依赖自动创建的动态映射。错误或缺失的映射会导致部分失败或静默类型转换。所需信息:目标索引名称、校正后的兼容映射,以及部署特定的配置限制。 - 在自托管/ECH上:复制期间将目标索引的
-
构建并提交重新索引请求。对于可能耗时超过几秒的复制操作,或用户表明索引较大时,调用——响应会立即返回任务ID,而非阻塞等待。
POST /_reindex?wait_for_completion=false请求主体要点:- — 源索引或数据流(名称正确,请勿与
source.index混淆)。dest.index - — 步骤4中准备好的目标索引。
dest.index - — 仅在步骤3选择过滤子集时包含。
source.query - — 当重试部分完成的重新索引时使用。
conflicts: "proceed" - 可选调优参数:(批量大小)、
source.size(限速)、requests_per_second(仅适用于本地重新索引,按主分片并行处理——远程索引切勿使用)、slices=auto(在慢集群上延长存活时间)、scroll(测试运行)、max_docs(数据转换)、script(摄入增强)。dest.pipeline
过滤子集示例(仅2025年1月数据):json{ "source": { "index": "eval-reindex-src", "query": { "range": { "@timestamp": { "gte": "2025-01-01", "lt": "2025-02-01" } } } }, "dest": { "index": "eval-reindex-jan" } }当任务为过滤子集复制或直接文档迁移时,请勿使用、_split或快照/恢复——这些API用于解决其他问题。_shrink -
跟踪任务直至完成。保存重新索引响应中的任务ID,轮询直到
GET /_tasks/{task_id}变为completed。读取true、status.total和status.created。在自托管/ECH上,还可使用response.failures列出活跃的重新索引任务;在Serverless上,仅支持通过任务ID查询(不支持列出/取消任务)。无需取消任务即可通过GET /_tasks?actions=*reindex&detailed调整限速。POST /_reindex/{task_id}/_rethrottle?requests_per_second=N -
验证并报告目标索引文档数量。调用(适用于所有部署类型)。在自托管/ECH上,还可使用
GET /{dest}/_count。将源索引过滤后的预期数量与目标索引数量进行比较。报告目标索引的精确数量——请勿估算或猜测。GET /_cat/count/{dest}?h=count成功完成完整复制后,通过恢复目标索引的生产环境配置(自托管/ECH上恢复副本数和刷新间隔;Serverless上仅恢复刷新间隔)。PUT /{dest}/_settings
Deployment constraints
部署限制
| Capability | Self-Managed / ECH | Serverless |
|---|---|---|
| Local reindex | Full support | Full support |
| Reindex from remote | Full support | Tech Preview — ECH remotes only |
| User-configurable | Managed — omit on index creation |
| Supported | Supported for local reindex |
| Supported | Not available — use |
| Full | Get by task id only |
| Supported | Blocked |
| Supported | Not available |
| 功能 | 自托管 / ECH | Serverless |
|---|---|---|
| 本地重新索引 | 完全支持 | 完全支持 |
| 远程重新索引 | 完全支持 | 技术预览——仅支持ECH远程 |
| 用户可配置 | 由Elastic管理——创建索引时无需设置 |
| 支持 | 支持本地重新索引 |
| 支持 | 不可用——使用 |
| 完全支持 | 仅支持通过任务ID查询 |
| 支持 | 已禁用 |
| 支持 | 不可用 |
Consider alternatives first
优先考虑替代方案
- Runtime fields — fix field-type mismatches or add computed fields without reindexing when stored values need not change.
- Aliases — redirect queries transparently; combine with reindex for zero-downtime mapping changes.
- Snapshot and restore (Self-Managed / ECH) — faster whole-index transfer when no transformation is needed.
See the decision tree in references/patterns.md.
- 运行时字段 — 当存储值无需更改时,可修复字段类型不匹配问题或添加计算字段,无需重新索引。
- 别名 — 透明重定向查询;结合重新索引可实现零停机映射变更。
- 快照与恢复(自托管/ECH) — 无需数据转换时,全索引传输速度更快。
请参阅references/patterns.md中的决策树。
Reference material
参考资料
- API parameter reference — full body and query parameters
POST /_reindex - Multi-step patterns — mapping changes, remote migration, merge, ingest pipeline, performance
- Tuning — batch size (), timestamps, versioning
source.size - Troubleshooting — mapping conflicts, scroll timeouts, count mismatches
- API参数参考 — 完整的请求主体和查询参数
POST /_reindex - 多步骤模式 — 映射变更、远程迁移、合并、摄入管道、性能优化
- 调优指南 — 批量大小()、时间戳、版本控制
source.size - 故障排除 — 映射冲突、滚动超时、数量不匹配
Examples
示例
"Copy into a new index with a corrected mapping" — create the destination first, then reindex:
logs-2024json
POST /_reindex
{ "source": { "index": "logs-2024" }, "dest": { "index": "logs-2024-v2" } }"Reindex a large index in parallel and throttle it" — slice automatically and cap the request rate:
json
POST /_reindex?slices=auto&requests_per_second=2000
{ "source": { "index": "events" }, "dest": { "index": "events-v2" } }"Migrate only recent documents" — filter the source with a query:
json
POST /_reindex
{
"source": { "index": "metrics", "query": { "range": { "@timestamp": { "gte": "now-30d" } } } },
"dest": { "index": "metrics-recent" }
}"将复制到具有修正映射的新索引中" — 先创建目标索引,再执行重新索引:
logs-2024json
POST /_reindex
{ "source": { "index": "logs-2024" }, "dest": { "index": "logs-2024-v2" } }"并行重新索引大型索引并限速" — 自动分片并限制请求速率:
json
POST /_reindex?slices=auto&requests_per_second=2000
{ "source": { "index": "events" }, "dest": { "index": "events-v2" } }"仅迁移近期文档" — 使用查询过滤源索引:
json
POST /_reindex
{
"source": { "index": "metrics", "query": { "range": { "@timestamp": { "gte": "now-30d" } } } },
"dest": { "index": "metrics-recent" }
}Guidelines
指南
- Confirm deployment type first. Call and read
GET /; shard, replica, cluster-settings, and task APIs differ between Self-Managed / ECH and Serverless (see Deployment constraints).build_flavor - Prefer an alternative when it fits. Runtime fields, aliases, or snapshot-and-restore often avoid a full reindex.
- Tune the destination for the copy. On Self-Managed / ECH set and
number_of_replicas: 0during the copy, then restore production settings afterward; on Serverless these are managed.refresh_interval: "-1" - Parallelize large copies. Use for local reindex and throttle with
slices=autoto protect the cluster.requests_per_second - Run big jobs asynchronously. Submit with and poll the task instead of blocking.
wait_for_completion=false - Verify by count. Compare the source filter expectation to the exact destination — never estimate.
GET /{dest}/_count
- 首先确认部署类型。调用并读取
GET /;自托管/ECH与Serverless的分片、副本、集群设置和任务API存在差异(请参阅部署限制)。build_flavor - 适合时优先选择替代方案。运行时字段、别名或快照与恢复通常可避免全量重新索引。
- 为复制操作调优目标索引。在自托管/ECH上,复制期间将设为
number_of_replicas、0设为refresh_interval,复制完成后恢复生产环境配置;Serverless上这些配置由系统管理。"-1" - 并行处理大型复制任务。对本地重新索引使用,并通过
slices=auto限速以保护集群。requests_per_second - 异步运行大型任务。使用提交任务,轮询任务状态而非阻塞等待。
wait_for_completion=false - 通过数量验证结果。将源索引过滤后的预期数量与目标索引的精确值进行比较——切勿估算。
GET /{dest}/_count
Operations
操作对照表
| HTTP API (shorthand) | |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| HTTP API(简写形式) | |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |