StackTraces
OpenSearch從零跑通

從零跑通 OpenSearch 索引模板與生命週期:6 步,外加 3 個容易卡住的地方

@debug_master2026-11-30適用 [email protected]

跟著這篇走完,你會學會「索引自動化」的兩件套:用索引模板給新建索引自動套上統一的 mapping 和 settings,用 ISM 策略讓索引按狀態自動流轉(比如 30 天后自動刪除)。整個過程 6 步,每步一條驗證命令。環境沿用從零跑通 OpenSearch 本地搜索服務:端口 9200、用戶 admin、密碼 Devfix@2026;你換了值替換掉就行。

先說清一個貫穿整篇的認知:模板和策略都只作用於「未來」,不回頭改舊索引。索引模板在「索引創建那一刻」套上設定,改模板不影響已存在的索引;ISM 策略在「滿足條件那一刻」觸發動作,靠週期掃描而不是實時。這篇預設你已經跑通了本地搜索服務,Docker 啟動、建索引、curl 帶 -sk -u admin:Devfix@2026 的寫法全部複用,不再重複。

從零跑通 OpenSearch 索引模板與生命週期 6 步流程封面:建模板 → 驗證自動套用 → 優先級 → 建策略 → 綁定 → 收場

前置條件

項 要求 說明
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 就滿足條件。報連接失敗說明容器沒起來,回第一篇第二步。

第一步:建索引模板 logs_template,套統一的 mapping 和 settings

日誌這類索引每天新建一個(logs-2026-01-01、logs-2026-01-02……),如果每次都手寫一遍 mapping,既煩又容易漏。索引模板解決這個:它把 settings、mappings、aliases 預設好,凡是名字匹配 index_patterns 的新建索引自動套上。

建一個 logs_template,匹配所有 logs-* 索引,套統一的 mapping(timestamp 日期、message 文本、level 關鍵詞)和 settings(1 主分片、0 副本):

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/_index_template/logs_template?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "index_patterns": [ "logs-*" ],
    "priority": 10,
    "template": {
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
      },
      "mappings": {
        "properties": {
          "timestamp": { "type": "date" },
          "message":   { "type": "text" },
          "level":     { "type": "keyword" }
        }
      }
    }
  }'
{
  "acknowledged" : true
}

兩個關鍵點:

  • index_patterns 是通配符模式。logs-* 匹配所有 logs- 開頭的索引名。它不含 :、"、+、/、\、|、?、#、>、< 這些字符。
  • priority 決定多個模板衝突時誰生效,越大越優先。先記著,第三步展開。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/_index_template/logs_template?pretty"
{
  "index_templates" : [
    {
      "name" : "logs_template",
      "index_template" : {
        "index_patterns" : [ "logs-*" ],
        "priority" : 10
      }
    }
  ]
}

能看到 logs_template 和它的 index_patterns 就通過。

第二步:驗證模板自動應用——建 logs-2026-01 索引

建一個匹配 logs-* 的索引,看模板是不是自動套上了:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/logs-2026-01?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "logs-2026-01"
}

再查它的 mapping,確認模板字段都在:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/logs-2026-01/_mapping?pretty"
{
  "logs-2026-01" : {
    "mappings" : {
      "properties" : {
        "level" : { "type" : "keyword" },
        "message" : { "type" : "text" },
        "timestamp" : { "type" : "date" }
      }
    }
  }
}

timestamp、message、level 三個字段都在——模板自動套上了,你沒在建索引時寫一個字。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/logs-2026-01/_mapping?pretty" \
  | grep -o '"timestamp"'

能 grep 到 "timestamp" 就通過。

第三步:priority 優先級 + 手動 settings 覆蓋

一個索引名可能同時匹配多個模板(比如 logs-* 和 logs-error-* 都匹配 logs-error-2026-01)。這時 priority 高的模板勝出。建一個更具體的 logs_error 模板,給更高 priority:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/_index_template/logs_error?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "index_patterns": [ "logs-error-*" ],
    "priority": 100,
    "template": {
      "mappings": {
        "properties": {
          "error_code": { "type": "keyword" }
        }
      }
    }
  }'
{
  "acknowledged" : true
}

現在 logs-error-* 的索引會套 logs_error(priority 100)而不是 logs_template(priority 10)。priority 越大越優先,不設就是 0(最低)。所以「越具體的模板給越高 priority」是鐵律,見常見問題第 3 項。

還有一點:建索引請求裡手動指定的 settings/mappings 會覆蓋模板。比如建索引時顯式給 number_of_shards: 3,就壓過模板裡的 1:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/logs-special-2026-01?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "settings": { "number_of_shards": 3 } }'

這個索引的 number_of_shards 是 3,不是模板的 1——手動指定優先於模板。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/logs-special-2026-01/_settings?pretty" \
  | grep -o '"number_of_shards" : "3"'

能 grep 到 "number_of_shards" : "3" 就通過。

第四步:建 ISM 策略 logs_retention——索引生命週期(承重步)

這是全文承重的一步。模板解決「新建索引長什麼樣」,ISM(Index State Management)解決「索引活著的時候怎麼自動流轉」。ISM 策略用三件套描述生命週期:

  • states(狀態):索引可以處於的狀態,比如 hot(熱)、delete(刪)。
  • actions(動作):進入某狀態時執行的操作,比如 delete 動作刪除索引。
  • transitions(轉移條件):從當前狀態轉移到下一個狀態的條件,比如「活了 30 天」。

建一個 logs_retention 策略:預設在 hot 狀態,索引活了 30 天后轉移到 delete 狀態、執行刪除:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/_plugins/_ism/policies/logs_retention?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "policy": {
      "description": "delete logs older than 30 days",
      "default_state": "hot",
      "states": [
        {
          "name": "hot",
          "actions": [],
          "transitions": [
            { "state_name": "delete", "conditions": { "min_index_age": "30d" } }
          ]
        },
        {
          "name": "delete",
          "actions": [ { "delete": {} } ],
          "transitions": []
        }
      ]
    }
  }'
{
  "_id" : "logs_retention",
  "_version" : 1
}

逐塊說清三件套:

  • default_state: "hot":新綁定這個策略的索引,初始狀態是 hot。
  • hot 狀態:actions 空(什麼都不做),transitions 裡有一條「min_index_age: 30d 時轉移到 delete」——即索引活了 30 天后進入刪除狀態。
  • delete 狀態:actions 裡一個 delete 動作(真正刪索引),transitions 空(終態,不再流轉)。

條件有幾種:min_index_age(索引年齡)、min_doc_count(文檔數)、min_size(主分片總大小)、min_state_age(在當前狀態停留時長)、cron(定時)。時間用時間單位(30d),大小用字節單位(50gb)。ISM 按順序評估 transitions,用第一個為 true 的。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/policies/logs_retention?pretty" \
  | grep -o '"logs_retention"'

能 grep 到 "logs_retention" 就通過。

第五步:綁定策略 + 驗證狀態

策略建好了,但不會自動作用到索引——要顯式綁定。把 logs-2026-01 綁上 logs_retention:

curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_plugins/_ism/add/logs-2026-01?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "policy_id": "logs_retention" }'
{
  "updated_indices" : 1,
  "failures" : false
}

現在查這個索引在策略裡的狀態:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/explain/logs-2026-01?pretty"

返回裡能看到它綁了 logs_retention 策略、當前在 hot 狀態。本地演示時 30d 還沒到,索引會一直停在 hot;生產上滿 30 天,ISM 按 job_interval(預設幾分鐘)週期檢查到條件滿足,就把它轉到 delete 狀態、執行刪除。

兩個要點:

  • 不綁定,策略是「沒人用的模板」。要麼像上面手動 _ism/add,要麼給策略加 ism_template(index_patterns + priority),讓匹配的新建索引自動綁上策略——見常見問題第 2 項。
  • 本地驗證別等 30 天。把 min_index_age 換成 min_state_age: 1m 之類短時間就能很快看到流轉。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/_plugins/_ism/explain/logs-2026-01?pretty" \
  | grep -o '"logs_retention"'

能 grep 到 "logs_retention"(說明綁上了策略)就通過。

第六步:收場

演示用的模板、索引、策略刪掉即可:

curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/logs-2026-01?pretty"
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/logs-special-2026-01?pretty"
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/_index_template/logs_template?pretty"
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/_index_template/logs_error?pretty"
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/_plugins/_ism/policies/logs_retention?pretty"
{
  "acknowledged" : true
}

核心概念

這張圖把兩件事擺清楚:索引模板在「索引創建時」給匹配 index_patterns 的新索引套上 settings/mappings/aliases;ISM 策略用 states/actions/transitions 三件套,讓索引按條件從一個狀態流到下一個狀態、自動執行動作(如刪除)。兩者都是「面向未來」的自動化,不回頭改舊索引。

OpenSearch 索引模板與生命週期鉛筆手繪詳解圖:左側定義卡解釋什麼是索引模板與 ISM,中間橫排卡片對應正文六步,底部詳解模板自動套用與 ISM 狀態流轉兩條主線

概念 是什麼 關鍵點
索引模板(index template) 給匹配 index_patterns 的新建索引套 settings/mappings/aliases 只在創建時應用,不回頭改舊索引
priority 多模板衝突時誰生效 越大越優先,不設是 0
ISM 策略(policy) 描述索引生命週期(狀態/動作/轉移) states + actions + transitions
transition 條件 觸發狀態轉移的條件 min_index_age/min_size/min_doc_count 等
綁定(_ism/add) 把策略掛到索引上 不綁定策略不生效

要點回顧

  • 模板只作用於新建索引——匹配 index_patterns 的索引在創建時套模板,改模板不回改已存在的索引。
  • priority 越大越優先——多模板都匹配時,priority 高的勝出;越具體的模板給越高 priority。
  • ISM 三件套 = states + actions + transitions——進入狀態觸發動作、條件滿足轉移到下一狀態,索引從 hot 自動流到 delete。
  • 策略要綁定才生效——手動 _ism/add 或策略里加 ism_template 自動套;ISM 按 job_interval 週期檢查,不是實時。

常見問題

建了模板,新建索引沒套上

現象:建了索引模板,新建索引卻沒套上 mapping/settings。

原因:index_patterns 不匹配索引名(模式寫錯),或模板只在創建時應用(改模板不影響已存在索引)。

相關文章:OpenSearch 索引模板沒生效:pattern 不匹配或只在創建時應用

ISM 策略建了,索引遲遲沒執行動作

現象:建了 ISM policy,索引卻遲遲沒按策略刪除/rollover。

原因:策略沒綁定到索引、轉移條件沒滿足(如 30 天沒到)、或還沒到 job_interval 檢查週期。

相關文章:OpenSearch ISM 策略沒執行:沒綁定、條件沒滿足或 job_interval 沒到

一個索引名匹配多個模板,套錯模板

現象:索引名同時匹配多個模板,套上的不是想要的那個。

原因:多個模板匹配時 priority 最高的被選;沒設 priority 都按 0 處理,結果不可控。

相關文章:OpenSearch 索引套錯模板:多模板匹配時靠 priority 定優先級

下一步

模板套不上舊索引?走零停機重建

模板不回頭改舊索引。要給舊索引改 mapping/settings,就「建新索引 + reindex + 別名切換」。

從零跑通 OpenSearch 索引別名與零停機重建

模板裡的 mapping 決定搜索效果

模板套的 mapping(text/keyword/date 類型、分詞器)直接決定後面的搜索、排序、聚合效果。

從零跑通 OpenSearch 相關性排序

生命週期之外,集群健康是另一塊運維

索引模板、ISM 是「索引級」運維;再往上還有多節點集群、健康狀態(green/yellow/red)、分片分配。

從零跑通 OpenSearch 本地搜索服務

卡在哪一步了?

從零跑通的過程中撞到報錯?把命令和輸出記下來,幫助下一個剛起步的人。

貢獻一條解法