跟着这篇走完,你会学会给索引「改名、改 mapping、改分析器」而不停服务的标准姿势:给索引起一个别名,应用层永远只认别名;要改结构时,建一个新索引、用 reindex 把数据迁过去、再用 _aliases 的 Index swapping 把别名原子地切到新索引。整个过程 6 步,每步一条验证命令。环境沿用从零跑通 OpenSearch 本地搜索服务:端口 9200、用户 admin、密码 Devfix@2026;你换了值替换掉就行。
先说清一个贯穿整篇的认知:索引名一旦建好就改不了 mapping,但别名可以随时重新指向。所以「改结构」的正确思路不是「原地改索引」,而是「建新的、迁数据、别名切过去、删旧的」——应用层从头到尾只认别名,底层索引换掉了它也不知道,这就是「零停机」。这篇默认你已经跑通了本地搜索服务,Docker 启动、建索引、bulk 导入、curl 带 -sk -u admin:Devfix@2026 的写法全部复用,不再重复。

前置条件
| 项 | 要求 | 说明 |
|---|---|---|
| OpenSearch | 3.x(Docker latest 镜像) | 单节点,security 开启,HTTPS 端口 9200 |
| 前置文章 | 已跑通 opensearch-local-search | 复用容器、密码、curl 写法 |
| curl | 任意版本 | 全文用 curl 调 REST API |
先确认服务活着:
curl -sk -u admin:Devfix@2026 "https://localhost:9200"
返回里有 cluster_name 就满足条件。报连接失败说明容器没起来,回第一篇第二步。
第一步:建 movies 索引、灌数据、加别名 movies_alias
先建一个普通的 movies 索引(title 是 text),灌 5 条电影,然后给它起个别名 movies_alias:
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies?pretty" \
-H "Content-Type: application/json" \
-d '{
"settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 },
"mappings": {
"properties": {
"title": { "type": "text" },
"year": { "type": "integer" },
"genre": { "type": "keyword" }
}
}
}'
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies/_bulk?pretty" \
-H "Content-Type: application/x-ndjson" \
--data-binary '{"index":{"_id":"1"}}
{"title":"Let the Wind Rise","year":2013,"genre":"drama"}
{"index":{"_id":"2"}}
{"title":"Gone with the Wind","year":1939,"genre":"drama"}
{"index":{"_id":"3"}}
{"title":"The Rise of Machines","year":2024,"genre":"sci-fi"}
{"index":{"_id":"4"}}
{"title":"Windmill Valley","year":2020,"genre":"drama"}
{"index":{"_id":"5"}}
{"title":"The Martian","year":2015,"genre":"sci-fi"}
'
{
"errors" : false,
"items" : [ { "index" : { "_id" : "1", "status" : 201 } }, ... ]
}
现在加别名。用 POST /_aliases,actions 里放一个 add:index 是真实索引名,alias 是你要起的别名:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_aliases?pretty" \
-H "Content-Type: application/json" \
-d '{ "actions": [ { "add": { "index": "movies", "alias": "movies_alias" } } ] }'
{
"acknowledged" : true
}
关键点:index 和 alias 别搞反——index 是真实索引 movies,alias 是别名 movies_alias。搞反了会给一个不存在的索引加别名,查别名时报 index_not_found,见常见问题第 3 项。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/_alias/movies_alias?pretty"
{
"movies" : {
"aliases" : {
"movies_alias" : { }
}
}
}
返回里 movies 带 movies_alias,就通过。
第二步:用别名读写——验证别名的「透明代理」
别名就像一个「指向索引的指针」。用别名查、用别名写,和直接用索引名效果一样:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_alias/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "query": { "match": { "title": "wind" } } }'
{
"hits" : {
"total" : { "value" : 3, "relation" : "eq" },
"hits" : [
{ "_id" : "1", "_source" : { "title" : "Let the Wind Rise" } },
{ "_id" : "2", "_source" : { "title" : "Gone with the Wind" } },
{ "_id" : "4", "_source" : { "title" : "Windmill Valley" } }
]
}
}
用别名 movies_alias 查,命中和直接用 movies 一模一样。别名就是一层透明的代理——这正是零停机重建的基础:应用层只写死别名 movies_alias,底下索引换成谁都无所谓。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_alias/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
返回 "count": 5 就通过。
第三步:建新索引 movies_v2(改 mapping)+ reindex 迁数据
现在想改结构——比如给 title 加一个 keyword 子字段(用于精确匹配和聚合)。但索引的 mapping 建好了不能改(text 类型改不动),所以建一个新索引 movies_v2,带上新 mapping:
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_v2?pretty" \
-H "Content-Type: application/json" \
-d '{
"settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 },
"mappings": {
"properties": {
"title": { "type": "text", "fields": { "raw": { "type": "keyword" } } },
"year": { "type": "integer" },
"genre": { "type": "keyword" }
}
}
}'
新索引 movies_v2 的 title 多了一个 title.raw(keyword 子字段)。接着用 reindex 把 movies 的数据迁过去:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_reindex?pretty" \
-H "Content-Type: application/json" \
-d '{ "source": { "index": "movies" }, "dest": { "index": "movies_v2" } }'
{
"took" : 118,
"total" : 5,
"created" : 5,
"updated" : 0,
"deleted" : 0
}
两个关键点:
reindex的源和目标必须不同。它不能「原地重建」,source和dest写同一个索引会报错——见常见问题第 2 项。- 目标索引要提前建。
reindex不会自动复制源索引的 mapping,movies_v2必须先用新的 mapping 建好,reindex 只负责搬数据。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_v2/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
movies_v2 也有 "count": 5,数据迁过来了。
第四步:Index swapping——别名原子切换(承重步)
这是全文承重的一步。现在 movies(旧)和 movies_v2(新)两个索引都活着,但别名 movies_alias 还指向旧的 movies。零停机的关键,是把别名从旧索引切到新索引,而且要在一个原子操作里完成——这就叫 Index swapping:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_aliases?pretty" \
-H "Content-Type: application/json" \
-d '{ "actions": [
{ "add": { "index": "movies_v2", "alias": "movies_alias" } },
{ "remove_index": { "index": "movies" } }
] }'
{
"acknowledged" : true
}
这个请求做了两件事,在一个原子事务里:
add:把别名movies_alias加到新索引movies_v2上。remove_index:删掉旧索引movies(连同它上面的别名一起)。
因为是一次原子操作,别名从旧索引「瞬间」切到新索引,中间没有任何「别名指向空」或「别名指向两个索引」的窗口。应用层从头到尾都在读写 movies_alias,这一刻之后它查到的就是 movies_v2 的新结构——零停机。这就是官方文档里「atomically replace an index with a new one without downtime」说的那件事。
对比一下「不原子」的错误做法:如果先 remove 旧索引的别名、再 add 新索引的别名,中间有一小段时间别名悬空,应用会报 index_not_found。所以切换必须放同一个请求里。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/_alias/movies_alias?pretty"
{
"movies_v2" : {
"aliases" : {
"movies_alias" : { }
}
}
}
现在别名指向 movies_v2 了,旧的 movies 已删。就通过。
第五步:验证零停机效果 + 多索引别名 is_write_index
先验证别名真的切到了新索引——用别名查,命中的 title 应该带 raw 子字段的能力(不过查询结果看不出 mapping 差异,我们用 _mapping 看):
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_alias/_mapping?pretty"
{
"movies_v2" : {
"mappings" : {
"properties" : {
"title" : { "type" : "text", "fields" : { "raw" : { "type" : "keyword" } } },
"year" : { "type" : "integer" },
"genre" : { "type" : "keyword" }
}
}
}
}
别名查到的 mapping 已经是 movies_v2 的新结构(带 title.raw)了。
再看一眼「别名指向多个索引」的情况——这在切换过程中、或者按月分索引的场景里会出现。一个别名同时指向多个索引时,写入必须有唯一目标,用 is_write_index 指定:
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_aliases?pretty" \
-H "Content-Type: application/json" \
-d '{ "actions": [ { "add": { "index": "movies_v2", "alias": "movies_alias", "is_write_index": true } } ] }'
is_write_index: true 告诉引擎「往 movies_alias 写的数据,进 movies_v2」。如果不设,多索引别名只能读不能写,一写就报 no write index is defined——见常见问题第 1 项。不过零停机重建的切换通常一步到位(add + remove_index 原子完成),不会长时间停在多索引状态,所以这个参数主要用在「多个索引长期共用一个别名」的场景。
验证命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_alias/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
别名 movies_alias 一直返回 "count": 5,全程没断。就通过。
第六步:收场
演示索引删掉即可:
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies_v2?pretty"
{
"acknowledged" : true
}
核心概念
这张图把零停机重建的流程摆清楚:应用层永远读写别名 movies_alias,别名指向某个真实索引;要改结构时,建新索引 movies_v2、reindex 迁数据、再用 _aliases 的 add + remove_index 原子切换,别名瞬间指向新索引,应用无感。

| 概念 | 是什么 | 关键点 |
|---|---|---|
| 索引别名(alias) | 一个名字指向一个或多个索引的映射 | 应用层只认别名,底层索引可换 |
_aliases API |
管理别名的入口(POST /_aliases) |
actions:add / remove / remove_index |
reindex |
把数据从源索引迁到目标索引 | 源和目标必须不同,目标先建 |
| Index swapping | 原子切换:add 新索引 + remove_index 旧索引 |
零停机核心,一次原子操作 |
is_write_index |
多索引别名指定写入目标 | 不设则别名只读不写 |
要点回顾
- 应用层只认别名,底层索引随便换——别名是「名字 → 索引」的指针,这是零停机重建的根基。
- 改 mapping 的正确姿势是「建新 → reindex → 别名切 → 删旧」——索引的 mapping 建好改不了,只能重建。
- Index swapping 是原子操作——
add新索引别名 +remove_index旧索引放同一请求,别名瞬间切换、无悬空窗口。 reindex源和目标必须不同、目标先建——不能原地重建,目标要带新 mapping 先建好。
常见问题
往多索引别名写数据报错
现象:一个别名指向多个索引,往别名写数据被拒绝。
原因:多索引共用别名时,必须用 is_write_index: true 指定哪个是写入目标;不指定,别名只能读不能写。
reindex 报「源和目标不能相同」
现象:reindex 的 source 和 dest 写同一个索引,直接报错。
原因:reindex 是「从 A 复制到 B」,源和目标必须不同;要重建就建新索引迁过去。
用别名查询报 index_not_found
现象:加了别名,用别名查询却报 no such index。
原因:别名名写错(大小写/符号)、add 里 index 和 alias 方向搞反、或别名指向的索引被删了。
