StackTraces
OpenSearch已驗證

OpenSearch 改了同義詞分析器,舊文檔搜索行為沒變

@debug_master13 天前更新閱讀 6 min適用 [email protected]00

給已有索引加同義詞過濾器、或改 analysis 設置後,搜新詞還是搜不到舊文檔,行為跟沒改一樣。根因不是設定寫錯,而是分析器作用在索引期:改分析器要 close 索引,而且已經寫入的舊文檔不會自動用新分析器重新分詞,必須 reindex。適用 OpenSearch 全版本。

Issue & Env(案發現場與環境)

  • 業務場景:跟著 從零跑通 OpenSearch 同義詞與停用詞 做完,後來想給 movies_dict 的 synopsis 字段再加一組同義詞 "movie, motion picture"。直接 PUT /movies_dict/_settings 更新 analysis,然後搜「motion picture」,舊文檔一條都不出來。
  • 環境:OpenSearch 3.x(Docker latest),單節點,索引 movies_dict,synopsis 用自定義分析器 text_analyzer,security 開啟。
  • 關鍵區分:設置可能寫進去了(GET /movies_dict/_settings 能讀到),但舊文檔的分詞結果還是舊的。

StackTrace(報錯原文 / 錯誤現象)

給未關閉的索引直接改 analysis,OpenSearch 會拒絕:

{
  "error": {
    "type": "illegal_argument_exception",
    "reason": "Can't update non dynamic settings [[index.analysis.analyzer.text_analyzer...]] for open indices"
  }
}

如果先把索引 close 再改,設置能寫進去、也不報錯,但搜新同義詞依然命中不了舊文檔——這是更隱蔽的第二段:設置生效了,索引裡的詞沒重建。

Reproduce(復現程式碼)

# ① 对 open 索引直接改 analysis → 报错
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict/_settings?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] } } } }'

# ② close 后改 settings → 能写入
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_dict/_close?pretty"
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict/_settings?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] } } } }'
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_dict/_open?pretty"

# ③ 搜新同义词「motion picture」→ 旧文档仍不命中(词没重建)
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "motion picture" } } }'

判定標準:GET /movies_dict/_settings 裡能看到新 filter,但搜新同義詞命中 0——就是「設置寫了、詞沒重建」。

Root Cause(根因分析)

分析器在索引期分詞,是「一次性」動作

同義詞是分析器裡的 token filter。文檔寫入時,分析器把 synopsis 切成 token 存進倒排索引;「film」那一刻被擴展成「film + movie」寫進了倒排索引。之後改分析器,只改變「以後寫入的文檔」怎麼分詞,已經寫進去的 token 不會自動重算。

改 analysis 是「非動態」設置,要先 close

分析器(index.analysis.*)屬於非動態索引設置,open 狀態下改會報 Can't update non dynamic settings。正確順序是 _close → PUT _settings → _open。這一步只是讓設置寫入成功,和舊文檔重建是兩碼事。

舊文檔必須 reindex 才吃新分析器

要讓舊文檔按新分析器重新分詞,必須 _reindex 重建(reindex 不能寫回同一索引,需建新索引再從舊索引遷數據)。只 close/reopen 不會觸發任何分詞重算。

The Fix(解決方案)

方案一:重建索引 + reindex(標準做法)

# 1) 建新索引,带完整新分析器
curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict_v2?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "settings": { "analysis": { "filter": { "my_synonyms": { "type": "synonym", "synonyms": ["film, movie, motion picture"] }, "my_stop": { "type": "stop", "stopwords": "_english_" } }, "analyzer": { "text_analyzer": { "type": "custom", "tokenizer": "standard", "filter": ["lowercase", "my_synonyms", "my_stop"] } } } },
    "mappings": { "properties": { "synopsis": { "type": "text", "analyzer": "text_analyzer" } } }
  }'

# 2) 旧索引迁到新索引
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/_reindex?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "source": { "index": "movies_dict" }, "dest": { "index": "movies_dict_v2" } }'

# 3) 验证 + 切名(删旧、别名指向新,或直接删旧)
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies_dict?pretty"

方案二:close → 改 settings → reopen(隻影響新文檔)

如果只關心「以後寫入的文檔」,close/reopen 改 settings 即可;但舊文檔不會變,別指望它。

注意事項

  • 改分析器前先想清楚要不要舊數據:要,走 reindex;不要,close/reopen 足夠。
  • reindex 不能寫回源索引,必須換目標索引。
  • 完整同義詞/停用詞設定見 從零跑通 OpenSearch 同義詞與停用詞。

References(參考來源)

最後更新於 2026-11-17

這篇幫到你了嗎?

剛解決了一個棘手的報錯?花兩分鐘記錄下來,幫助下一個遇到同樣問題的開發者。

貢獻一條解法