StackTraces
OpenSearch從零跑通

從零跑通 OpenSearch 同義詞與停用詞:6 步,外加 3 個容易卡住的地方

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

跟著這篇走完,你會在本機 OpenSearch 上把「同義詞」和「停用詞」這兩把詞典層的扳手都擰一遍:定義一個帶 synonym 和 stop 兩個 token filter 的自定義分析器,用 _analyze 親眼看「film 擴展出 movie」「the 被剔除」,再真實搜索驗證「搜 film 能帶出寫 movie 的文檔」「搜 the 返回 0 命中」。整個過程 6 步,每步一條驗證命令。環境沿用從零跑通 OpenSearch 本地搜索服務:端口 9200、用戶 admin、密碼 Devfix@2026;你換了這些值替換掉就行。

先糾正一個從 Meilisearch 帶過來的直覺:Meilisearch 的同義詞和停用詞是獨立的 settings 端點(/settings/synonyms、/settings/stop-words),改完自動全量重索引。OpenSearch 不是這麼幹的——它倆是自定義分析器(analyzer)裡的兩個 token filter,寫進索引的 analysis 設置,作用在索引期。這個心智模型的差異,是整篇的暗線。如果你跑過從零跑通 Meilisearch 同義詞與停用詞,邊看邊對比會特別有感覺。

從零跑通 OpenSearch 同義詞與停用詞 6 步流程封面:建索引與分析器 → _analyze 看 token 流 → 搜 film 帶出 movie → => 單向替換 → 停用詞驗證 → 收場

前置條件

項 要求 說明
OpenSearch 3.x(Docker latest 鏡像) 單節點,security 開啟,HTTPS 端口 9200
前置文章 已跑通 opensearch-local-search 複用容器、密碼、curl 帶 -sk -u admin:Devfix@2026 的寫法
curl 任意版本 全文用 curl 調 REST API
記憶負擔 無 同義詞/停用詞命令都在建索引和 _analyze 裡,不換接口

先確認服務活著:

curl -sk -u admin:Devfix@2026 "https://localhost:9200"

返回裡有 cluster_name 就滿足條件。報連接失敗說明容器沒起來,回第一篇第二步。

第一步:建 movies_dict 索引,定義帶同義詞和停用詞的分析器,灌 5 條電影

同義詞和停用詞都作用在「分詞後的詞」上,而分詞這件事由分析器完成。所以第一步不是「配同義詞」,而是「造一個把這兩件事都做掉的分析器」。分析器 = 一個 tokenizer(分詞器)串一串 token filter(詞過濾器):先 standard 把文本切成詞,再 lowercase 統一小寫,再 my_synonyms 加同義詞,最後 my_stop 剔停用詞。

新建索引 movies_dict(和第一篇的 movies 分開,互不干擾),settings 裡定義兩個 filter 和一個自定義分析器:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_dict?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "settings": {
      "analysis": {
        "filter": {
          "my_synonyms": {
            "type": "synonym",
            "synonyms": ["film, movie", "laptop => computer"]
          },
          "my_stop": {
            "type": "stop",
            "stopwords": "_english_"
          }
        },
        "analyzer": {
          "text_analyzer": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["lowercase", "my_synonyms", "my_stop"]
          }
        }
      }
    },
    "mappings": {
      "properties": {
        "title":   { "type": "text" },
        "year":    { "type": "integer" },
        "genre":   { "type": "keyword" },
        "synopsis": { "type": "text", "analyzer": "text_analyzer" }
      }
    }
  }'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "movies_dict"
}

兩個關鍵點。第一,synonyms 裡兩條規則不是一種東西:"film, movie" 用逗號,是等價組(雙向);"laptop => computer" 用箭頭,是單向替換(搜 laptop 當成 computer,反向不通)。這條我們第四步專門拆。第二,只有 synopsis 字段掛了 text_analyzer,title 還是預設 standard——分析器定義在 settings 裡只是「註冊」,字段要用它必須寫 "analyzer": "text_analyzer",漏寫就等於沒配(見常見問題第 3 項)。

接著灌 5 條電影。synopsis 裡刻意埋了兩類東西,後面每一步靠它們顯形:「film」(id 1、4)和「movie」(id 2、3)這對同義詞、以及散落在各條裡的 a/the/an/in/on 這些停用詞:

curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_dict/_bulk?pretty" \
  -H "Content-Type: application/x-ndjson" \
  --data-binary '{"index":{"_id":"1"}}
{"title":"Let the Wind Rise","year":2013,"genre":"drama","synopsis":"a film about hope in the storm"}
{"index":{"_id":"2"}}
{"title":"Gone with the Wind","year":1939,"genre":"drama","synopsis":"a movie about survival"}
{"index":{"_id":"3"}}
{"title":"The Rise of Machines","year":2024,"genre":"sci-fi","synopsis":"a movie about robots taking over"}
{"index":{"_id":"4"}}
{"title":"Windmill Valley","year":2020,"genre":"drama","synopsis":"a film about village life"}
{"index":{"_id":"5"}}
{"title":"The Martian","year":2015,"genre":"sci-fi","synopsis":"an adventure on Mars"}
'
{
  "errors" : false,
  "items" : [ { "index" : { "_id" : "1", "status" : 201 } }, ... ]
}

errors 是 false、5 條都 201 就進去了。數據就位,但此刻你還不知道分析器到底把 synopsis 切成了什麼——這就是第二步的活。

驗證命令:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_dict/_count?pretty"
{
  "count" : 5,
  "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}

返回 "count": 5 就通過。

第二步:_analyze 對比——看懂 token filter 怎麼「加詞」和「減詞」(承重步)

這是全文承重的一步。同義詞是「加詞」、停用詞是「減詞」,這兩件事到底發生在哪、長什麼樣,用 _analyze 一眼看穿——它讓你不建索引、不搜數據,直接把一個分析器對一段文本的處理結果擺出來。

先看預設 standard 分析器怎麼切 "a film about hope in the storm"(這是沒配任何東西的基線):

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "standard", "text": "a film about hope in the storm" }'
{
  "tokens" : [
    { "token" : "a",     "position" : 0 },
    { "token" : "film",  "position" : 1 },
    { "token" : "about", "position" : 2 },
    { "token" : "hope",  "position" : 3 },
    { "token" : "in",    "position" : 4 },
    { "token" : "the",   "position" : 5 },
    { "token" : "storm", "position" : 6 }
  ]
}

standard 就是按空格切:a、film、about……一個不少,film 就是 film,the 還在。這是「什麼都沒配」的基線。

再換成我們自定義的 text_analyzer,同樣一句話:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a film about hope in the storm" }'
{
  "tokens" : [
    { "token" : "film",  "position" : 1, "type" : "<ALPHANUM>" },
    { "token" : "movie", "position" : 1, "type" : "SYNONYM" },
    { "token" : "about", "position" : 2, "type" : "<ALPHANUM>" },
    { "token" : "hope",  "position" : 3, "type" : "<ALPHANUM>" },
    { "token" : "storm", "position" : 4, "type" : "<ALPHANUM>" }
  ]
}

同一句話,兩件事同時發生:

  • 加詞:film 旁邊多了一個 movie,而且 type 是 SYNONYM、position 都是 1(和 film 同位置)——這是 my_synonyms 裡 "film, movie" 逗號等價組乾的:film 被擴展成 film + movie 兩個詞。
  • 減詞:a、in、the 三個詞不見了——這是 my_stop(_english_)乾的,把英文停用詞直接從 token 流裡剔除。

這就是整篇的核心:同義詞是「往 token 流里加等價詞」,停用詞是「從 token 流裡刪詞」,兩者都是分析器裡的一環,發生在索引期——文檔寫入時 synopsis 被這個分析器處理一遍,加完詞、刪完詞的結果才進倒排索引。不是「搜的時候臨時聯想」。

再看一眼 => 的「加詞」和逗號有什麼不同。_analyze 處理 "a laptop"(my_synonyms 裡有 "laptop => computer"):

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a laptop" }'
{
  "tokens" : [
    { "token" : "computer", "position" : 1, "type" : "SYNONYM" }
  ]
}

laptop 沒了,只剩 computer——=> 是「替換」不是「加詞」。這個區別第四步展開。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a film about hope in the storm" }' \
  | grep -o '"token" : "[a-z]*"'

能看到 film、movie、about、hope、storm 且沒有 a/the/in,就通過。

第三步:搜「film」帶出「movie」——同義詞雙向生效

第二步看到的是「詞被加了」,這一步看加的詞在真實搜索裡管不管用。搜 synopsis 裡的 film:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "film" } } }'
{
  "hits" : {
    "total" : { "value" : 4, "relation" : "eq" },
    "hits" : [
      { "_id" : "1", "_source" : { "title" : "Let the Wind Rise",    "synopsis" : "a film about hope in the storm" } },
      { "_id" : "2", "_source" : { "title" : "Gone with the Wind",   "synopsis" : "a movie about survival" } },
      { "_id" : "3", "_source" : { "title" : "The Rise of Machines", "synopsis" : "a movie about robots taking over" } },
      { "_id" : "4", "_source" : { "title" : "Windmill Valley",      "synopsis" : "a film about village life" } }
    ]
  }
}

命中 4 條。id 1、4 的 synopsis 本來寫的就是 film,id 2、3 寫的是 movie——它們被帶出來,靠的就是索引期 "film, movie" 把 movie 也擴展成了 film + movie。id 5(an adventure on Mars)既沒 film 也沒 movie,所以沒進來。

反過來搜 movie 也一樣命中這 4 條(id 1、4 被反向帶出)——逗號等價組是雙向的。這一步和 Meilisearch 有本質區別:Meilisearch 的同義詞預設單向(搜 A 帶出 B,搜 B 帶不出 A),而 OpenSearch 逗號預設雙向。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "movie" } } }' \
  | grep -o '"value" : [0-9]*'

返回 "value" : 4,兩個方向都是 4 條,就證明雙向。

第四步:=> 單向替換,和逗號的雙向對比

第三步的逗號是「雙向加詞」,=> 是「單向替換」。第二步已經瞄到 laptop 被替換成 computer,這一步把它和逗號並排看清,並說明它為什麼是單向。

先回顧逗號(等價組)對 "a film" 的處理——film 還在,movie 加進來:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a film" }'
{
  "tokens" : [
    { "token" : "film",  "position" : 1, "type" : "<ALPHANUM>" },
    { "token" : "movie", "position" : 1, "type" : "SYNONYM" }
  ]
}

film 和 movie 並存——所以搜 film 能命中含 movie 的文檔,搜 movie 也能命中含 film 的文檔。

再看 => 對 "a laptop"——laptop 被替換成 computer,laptop 自己沒了:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a laptop" }'
{
  "tokens" : [
    { "token" : "computer", "position" : 1, "type" : "SYNONYM" }
  ]
}

反向 "a computer" 呢?computer 不在任何規則的左側,原樣保留、不擴展:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a computer" }'
{
  "tokens" : [
    { "token" : "computer", "position" : 1, "type" : "<ALPHANUM>" }
  ]
}

對比很清晰:=> 的語義是「遇到左側的詞,當成右側處理」,只在一個方向生效;它不會自動產生「computer 也能搜出 laptop」的反向規則。想雙向,就用逗號等價組 "laptop, computer",或顯式再寫一條 "computer => laptop"。這正是新手最容易搞反的地方,見常見問題第 2 項。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_analyze?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "analyzer": "text_analyzer", "text": "a laptop" }' \
  | grep -o '"token" : "[a-z]*"'

只有 computer、沒有 laptop,就通過。

第五步:停用詞——搜「the」變 0 命中,正常查詢不誤傷

同義詞是「加詞」,停用詞是「減詞」。第二步已經看到 a、the、in 被 _english_ 剔除,這一步驗證它在搜索裡的效果:搜 the:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "the" } } }'
{
  "hits" : {
    "total" : { "value" : 0, "relation" : "eq" },
    "hits" : [ ]
  }
}

0 命中。要點明機制:停用詞是在索引期被移除的——the 壓根沒進倒排索引(倒排索引是一張「詞 → 含這個詞的文檔 id 列表」的表),所以引擎里根本沒有 the 這個詞可查。這不是「匹配之後降權」,是「詞根本不存在」。和同義詞「查詢/索引期擴展」是兩個作用時機。

再驗證它不誤傷正常查詢——搜 adventure(id 5 的 synopsis 裡只有 adventure 和 mars 兩個有效詞,an/on 都被剔了):

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "adventure" } } }'
{
  "hits" : {
    "total" : { "value" : 1, "relation" : "eq" },
    "hits" : [ { "_id" : "5", "_source" : { "title" : "The Martian", "synopsis" : "an adventure on Mars" } } ]
  }
}

照常命中 id 5。查詢裡的停用詞被剔除、實義詞繼續參與匹配,結果不受影響。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_dict/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "match": { "synopsis": "the" } } }' \
  | grep -o '"value" : [0-9]*'

返回 "value" : 0 就通過。

第六步:GET settings 全貌 + 收場

讀一下整個索引的 analysis 設置,確認兩個 filter 和自定義分析器都在:

curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies_dict/_settings?pretty"
{
  "movies_dict" : {
    "settings" : {
      "index" : {
        "analysis" : {
          "filter" : {
            "my_stop" : { "type" : "stop", "stopwords" : [ "_english_" ] },
            "my_synonyms" : { "type" : "synonym", "synonyms" : [ "film, movie", "laptop => computer" ] }
          },
          "analyzer" : {
            "text_analyzer" : { "type" : "custom", "tokenizer" : "standard", "filter" : [ "lowercase", "my_synonyms", "my_stop" ] }
          }
        }
      }
    }
  }
}

收場:刪掉這個演示索引即可(不影響 movies):

curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies_dict?pretty"
{
  "acknowledged" : true
}

核心概念

這張圖把同義詞和停用詞在分析管線裡的位置擺清楚:一段文本先被 standard 分詞器切成詞,再依次經過 lowercase(統一小寫)、my_synonyms(同義詞加詞/替換)、my_stop(停用詞減詞),最後得到的 token 才進倒排索引。同義詞和停用詞都是這條鏈上的一環,不是獨立開關。

OpenSearch 同義詞與停用詞鉛筆手繪詳解圖:左側定義卡解釋什麼是 token filter,中間橫排卡片對應正文六步,底部三個手繪方框分別詳解分詞器、同義詞加詞、停用詞減詞

概念 是什麼 類比
分析器(analyzer) 分詞器 + 一串 token filter 的組合,決定文本怎麼變成詞 一條流水線,原料進去,詞出來
token filter(詞過濾器) 分析器裡的一環,對 token 流做增刪改 流水線上的一道工序:這道加零件,那道拆零件
synonym filter 同義詞過濾器,逗號=等價組(雙向加詞),=>=單向替換 查字典的「參見」條:逗號是互相參見,=> 是從 A 指到 B
stop filter 停用詞過濾器,_english_ 是內置英文停用詞表 編目時直接不給「的/the」建卡片
_analyze API 不建索引、不搜數據,直接看分析器對文本的處理結果 拿段文字丟進流水線試跑,看出來的詞
索引期生效 同義詞加詞、停用詞減詞都發生在文檔寫入時,改分析器要 reindex 才對舊文檔生效 書入庫時編好目,之後改編目規則要重新入庫

要點回顧

  • 同義詞和停用詞是分析器裡的 token filter——不是獨立設置端點,寫進索引 settings.analysis,字段要用必須在 mapping 裡寫 "analyzer"。
  • 同義詞「加詞」、停用詞「減詞」,都發生在索引期——文檔寫入時處理完才進倒排索引;改分析器要 close + reindex 才對舊文檔生效。
  • 逗號是雙向等價組,=> 是單向替換——"film, movie" 兩個方向都通,"laptop => computer" 只把 laptop 換成 computer,反向不通。
  • _analyze 是排障第一工具——token 流裡有什麼、缺什麼,一眼就能定位,比搜數據猜方向快得多。

常見問題

改了分析器,舊文檔搜索行為沒變

現象:給已有索引加同義詞、或改 analysis 設置後,搜新詞還是搜不到舊文檔,跟沒改一樣。

原因:分析器作用在索引期,改分析器要先 close 索引,且舊文檔必須 reindex 才會按新分析器重新分詞,光改設置不重建等於沒改。

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

逗號和 => 方向搞反了

現象:寫 "laptop => computer" 指望雙向,搜 computer 帶不出 laptop;或寫 "film, movie" 指望單向,結果兩個方向都通了。

原因:Solr 格式裡逗號是等價組(expand 預設 true,雙向展開),=> 是單向替換(左側被右側替換)。兩種寫法不是一個意思,搞混方向就反了。

相關文章:OpenSearch 同義詞方向搞混:逗號和 => 不是一個意思

停用詞設了還能搜到

現象:配了 stop 過濾器後,搜停用詞還是能命中。

原因:通常是自定義分析器沒真正掛到字段映射上(mapping 沒寫 "analyzer"),或 search 端用了和 index 端不同的分析器,或自定義停用詞大小寫/分詞結果不匹配。

相關文章:OpenSearch 停用詞設了還能搜到:分析器沒掛到字段或 search 端不一致

下一步

詞從哪來:分詞與中文

同義詞和停用詞都作用在分詞結果上。英文靠 standard,中文要用 IK 插件分詞,切詞邊界直接決定這兩個 filter 的實際效果。

從零跑通 OpenSearch 中文搜索

同義詞只是加分項,排序才是主戰場

同義詞解決「能不能命中」,命中了誰排前面靠 BM25 打分和 explain 溯源。

從零跑通 OpenSearch 相關性排序

對稱篇:Meilisearch 同義詞與停用詞

同一件事在 Meilisearch 裡是獨立 settings 端點 + 自動重索引,OpenSearch 是分析器 token filter + 手動 reindex。兩篇對照讀,理解更深。

從零跑通 Meilisearch 同義詞與停用詞

卡在哪一步了?

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

貢獻一條解法