跟著這篇走完,你會學會給索引「改名、改 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 方向搞反、或別名指向的索引被刪了。
