跟著這篇走完,你會給搜索框加上「你是不是想搜」(did you mean):用 term suggester 糾單個拼錯的單詞、用 phrase suggester 糾整句短語,suggest_mode 控制什麼時候給建議、max_edits 和 string_distance 調糾錯力度。整個過程 6 步,每步一條驗證命令。環境沿用從零跑通 OpenSearch 本地搜索服務:端口 9200、用戶 admin、密碼 Devfix@2026;你換了值替換掉就行。
先說清一個貫穿整篇的認知:拼寫糾錯不是「查字典」,而是「從索引字段裡找最接近的詞」。term suggester 用編輯距離(把錯詞改成正確詞需要增刪改幾個字符)逐詞糾,phrase suggester 用 n-gram 語言模型整句糾(考慮詞和詞的搭配)。這和從零跑通 OpenSearch 搜索建議與自動補全裡的 completion 補全是兩碼事:補全幫你「把沒打完的詞補完」,糾錯幫你「把打錯的詞改對」。這篇預設你已經跑通了本地搜索服務,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 索引,配好 trigram 分析器,灌數據
term suggester 用普通 text 字段就夠,但 phrase suggester 需要專門的 trigram(shingle)分析器字段。所以建索引時一次配齊:title 是 text(用於 term),另加一個 title.trigram 子字段(用於 phrase)。trigram 分析器用 shingle 過濾器,把相鄰詞拼成 n-gram:
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,
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"trigram": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "shingle" ] }
},
"filter": {
"shingle": { "type": "shingle", "min_shingle_size": 2, "max_shingle_size": 3 }
}
}
}
},
"mappings": {
"properties": {
"title": { "type": "text", "fields": { "trigram": { "type": "text", "analyzer": "trigram" } } },
"year": { "type": "integer" },
"genre": { "type": "keyword" }
}
}
}'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "movies"
}
灌 5 條電影:
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 } }, ... ]
}
errors 是 false 就進去了。title 用於 term 糾錯,title.trigram 用於 phrase 糾錯,兩個字段都備好了。
驗證命令:
curl -sk -u admin:Devfix@2026 "https://localhost:9200/movies/_count?pretty"
{
"count" : 5,
"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }
}
返回 "count": 5 就通過。
第二步:term suggester 基本用法——編輯距離糾單詞(承重步)
這是全文承重的一步。term suggester 糾正單個拼錯的詞,機制是編輯距離——把錯詞改成正確詞需要增、刪、改幾個字符。搜一個拼錯的 martain(正確是 martian),term suggester 從 title 字段裡找到最接近的詞:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martain", "term": { "field": "title" } } } }'
{
"suggest" : {
"spell-check" : [
{
"text" : "martain",
"offset" : 0,
"length" : 7,
"options" : [
{ "text" : "martian", "score" : 0.8, "freq" : 1 }
]
}
]
}
}
三個觀察點,是整篇的核心:
suggest返回結構和補全一樣分兩層。suggest下面是你起的名字spell-check,它是陣列;陣列裡元素有text(你輸入的錯詞)和options(糾錯建議列表)。建議在options[].text裡,這裡是martian。score是相似度,freq是詞頻。score由編輯距離算出來,越高越接近;freq是這個詞在索引裡的出現次數。martian的freq: 1(The Martian出現一次)。- 不需要特殊字段。term suggester 直接在普通
text字段上就能跑,因為它只比較單個詞。
再搜一個多建議的例子 the(拼對了)看看——先記住,第二步會解釋為什麼它不給建議:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martian", "term": { "field": "title" } } } }'
options 是空陣列——martian 拼對了、且在索引裡存在,預設不給建議。這是 suggest_mode 在起作用,見常見問題第 3 項。
驗證命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martain", "term": { "field": "title" } } } }' \
| grep -o '"text" : "martian"'
能 grep 到 "text" : "martian" 就通過。
第三步:suggest_mode——什麼時候給建議
預設 suggest_mode: missing,意思是「只對索引裡沒出現過的詞給建議」。martain 在 title 裡不存在,所以給建議;martian 存在,所以不給。三種模式:
missing(預設):只糾正索引裡沒有的詞。popular:只建議比輸入詞更頻繁的詞。always:對每個詞都給建議,不管拼沒拼對。
想「拼對的詞也給個建議」就換 always:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martian", "term": { "field": "title", "suggest_mode": "always" } } } }'
現在 martian 拼對了也會給建議。經典 did-you-mean 場景用預設 missing 就夠了——只在用戶真拼錯時才打擾他。
驗證命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martian", "term": { "field": "title", "suggest_mode": "always" } } } }' \
| grep -o '"options" : \[ {'
能 grep 到 "options" : [ {(說明有建議了)就通過。
第四步:max_edits + string_distance——調糾錯力度
兩個參數控制「糾多遠」:
max_edits:最大編輯距離,範圍 1–2,預設 2。拼得越離譜、編輯距離越大;設 1 就只糾「差一個字符」的錯。string_distance:用哪種編輯距離算法,預設internal(Damerau-Levenshtein 的優化版),還有levenshtein、jaro_winkler、ngram可選。
收緊到 max_edits: 1,只糾「差一個字符」的錯:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martain", "term": { "field": "title", "max_edits": 1, "string_distance": "levenshtein" } } } }'
max_edits: 1 限制編輯距離最多 1,string_distance: levenshtein 指定用 Levenshtein 算法(只算增刪改,不算字符交換)。日常用預設 internal + max_edits: 2 就夠,拼錯特別離譜時才需要放寬或換算法。
驗證命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "spell-check": { "text": "martain", "term": { "field": "title", "max_edits": 1 } } } }' \
| grep -o '"text" : "martian"'
仍能 grep 到 "text" : "martian"(編輯距離 1 內)就通過。
第五步:phrase suggester——糾整句短語
term 逐詞糾,不管詞和詞的搭配;phrase suggester 用 n-gram 語言模型糾整句,考慮「哪個詞該跟哪個詞」。搜拼錯的短語 the martain,phrase 返回糾正後的整句:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "phrase-check": { "text": "the martain", "phrase": { "field": "title.trigram" } } } }'
{
"suggest" : {
"phrase-check" : [
{
"text" : "the martain",
"offset" : 0,
"length" : 11,
"options" : [
{ "text" : "the martian", "score" : 0.5 }
]
}
]
}
}
注意兩點:
field是title.trigram,不是title。phrase suggester 靠第一步建的 trigram(shingle)字段算詞搭配,對普通text字段跑會報錯——見常見問題第 2 項。- 返回的是整句。
options[].text是the martian,不是一個詞一個詞拆開。
驗證命令:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "phrase-check": { "text": "the martain", "phrase": { "field": "title.trigram" } } } }' \
| grep -o '"text" : "the martian"'
能 grep 到 "text" : "the martian" 就通過。
第六步:phrase 參數 + 收場
phrase 有兩個常用參數:max_errors(最多允許多少個詞拼錯,預設 0.5 即一半)、confidence(置信度,預設 1.0,越高越保守)。收緊 max_errors 到 1:
curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies/_search?pretty" \
-H "Content-Type: application/json" \
-d '{ "suggest": { "phrase-check": { "text": "the martain", "phrase": { "field": "title.trigram", "max_errors": 1, "confidence": 1.0 } } } }'
max_errors: 1 表示整句最多一個詞拼錯才給建議,confidence: 1.0 表示只返回比原句打分更高的建議。日常短語短,預設就夠;短語長、錯詞多時再調 max_errors。
收場:演示索引用完刪掉即可:
curl -sk -u admin:Devfix@2026 -X DELETE "https://localhost:9200/movies?pretty"
{
"acknowledged" : true
}
核心概念
這張圖把兩條糾錯路線擺清楚:term suggester 用編輯距離(增刪改幾個字符)逐詞糾,普通 text 字段就夠;phrase suggester 用 n-gram 語言模型整句糾(考慮詞搭配),需要專門的 trigram(shingle)字段。前者糾「詞」,後者糾「句」。

| 概念 | 是什麼 | 關鍵點 |
|---|---|---|
| term suggester | 單詞級糾錯 | 編輯距離,普通 text 字段即可 |
| phrase suggester | 短語級糾錯 | n-gram 語言模型,需 trigram 字段 |
suggest_mode |
何時給建議 | missing/popular/always |
max_edits |
最大編輯距離 | 預設 2,範圍 1–2 |
string_distance |
編輯距離算法 | internal/levenshtein/jaro_winkler/ngram |
要點回顧
- term 糾單詞、phrase 糾整句——term 用編輯距離逐詞糾,phrase 用 n-gram 語言模型整句糾(考慮詞搭配)。
suggest_mode: missing預設只糾正「索引裡沒有的詞」——拼對的詞不給建議,想總給建議用always。max_edits管糾多遠、string_distance管用什麼算法——編輯距離越大糾得越遠,算法決定相似度怎麼算。- phrase 需要 trigram(shingle)字段——對普通
text字段跑 phrase 會報錯,要先建 trigram 子字段。
常見問題
term suggester 返回空 options
現象:搜了拼錯的詞,term suggester 卻返回空 options。
原因:suggest_mode 預設 missing 只對索引裡沒有的詞建議(拼對的不建議)、min_word_length 預設 4 太短不給、或拼錯超出 max_edits 編輯距離。
phrase suggester 報錯或沒結果
現象:用 phrase suggester 糾錯,報錯或沒結果。
原因:phrase suggester 需要 trigram(shingle)分析器字段,直接對普通 text 字段跑會失敗。
相關文章:OpenSearch phrase suggester 沒結果:字段沒建 trigram(shingle)分析器
建議時有時無,或給的建議不對
現象:term suggester 建議時有時無,或給的建議不符合預期。
原因:suggest_mode 三種模式(missing/popular/always)語義沒分清,選錯模式建議就時有時無。
相關文章:OpenSearch suggest_mode 理解錯:missing/popular/always 語義分不清
