StackTraces
OpenSearch從零跑通

從零跑通 OpenSearch 搜索建議與自動補全:6 步,外加 3 個容易卡住的地方

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

跟著這篇走完,你會把搜索框「邊輸入邊補全」這件事配齊:用 completion 字段存補全列表、用 suggest 端點做前綴匹配、用 size 控補全條數、用 fuzzy 讓拼錯的也能補全,最後用 search_as_you_type 補上「中間詞也能補全」的場景。整個過程 6 步,每步一條驗證命令。環境沿用從零跑通 OpenSearch 本地搜索服務:端口 9200、用戶 admin、密碼 Devfix@2026;你換了值替換掉就行。

先說清一個貫穿整篇的認知:自動補全不是「在普通字段上做前綴查詢」,而是換了一套專門的機制——completion 是獨立字段類型,索引期把補全列表灌進記憶體裡的一塊快速查找結構(FST),查詢走 suggest 端點、只做「文本開頭」的前綴匹配。這套機制和 _search 的普通查詢是兩條路,本篇預設你已經跑通了本地搜索服務,Docker 啟動、建索引、bulk 導入、curl 帶 -sk -u admin:Devfix@2026 的寫法全部複用,不再重複。

從零跑通 OpenSearch 搜索建議與自動補全 6 步流程封面:建補全索引 → suggest 前綴補全 → 控條數 → 模糊補全 → 中間詞補全 → 收場

前置條件

項 要求 說明
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_suggest 索引,suggest 字段用 completion 類型,灌補全列表

自動補全需要一個專門類型的字段——completion。它和 text、keyword 都不一樣:text 存的是「待搜索的正文」,completion 存的是「下拉框要補全出來的候選」。先建索引,suggest 字段用 completion:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_suggest?pretty" \
  -H "Content-Type: application/json" \
  -d '{
    "settings": { "index.number_of_shards": 1, "index.number_of_replicas": 0 },
    "mappings": {
      "properties": {
        "title":    { "type": "keyword" },
        "year":     { "type": "integer" },
        "genre":    { "type": "keyword" },
        "suggest":  { "type": "completion" }
      }
    }
  }'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "movies_suggest"
}

關鍵點:suggest 是 completion 類型,不是 text。completion 字段在索引期會把候選值建成記憶體 FST,查詢時直接查這個結構,所以快——但代價是「沒灌就沒有」。現在把 5 條電影的標題作為補全候選灌進去:

curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_suggest/_bulk?pretty" \
  -H "Content-Type: application/x-ndjson" \
  --data-binary '{"index":{"_id":"1"}}
{"title":"The Martian","year":2015,"genre":"sci-fi","suggest":"The Martian"}
{"index":{"_id":"2"}}
{"title":"The Matrix","year":1999,"genre":"sci-fi","suggest":"The Matrix"}
{"index":{"_id":"3"}}
{"title":"Windmill Valley","year":2020,"genre":"drama","suggest":"Windmill Valley"}
{"index":{"_id":"4"}}
{"title":"Gone with the Wind","year":1939,"genre":"drama","suggest":"Gone with the Wind"}
{"index":{"_id":"5"}}
{"title":"Let the Wind Rise","year":2013,"genre":"drama","suggest":"Let the Wind Rise"}
'
{
  "errors" : false,
  "items" : [ { "index" : { "_id" : "1", "status" : 201 } }, ... ]
}

errors 是 false、5 條都 201 就進去了。此刻補全列表已經進了記憶體 FST,第二步就來查它。

驗證命令:

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

返回 "count": 5 就通過。

第二步:completion suggester 基本用法——suggest + prefix(承重步)

這是全文承重的一步。補全不寫在 query 裡,而是用 suggest 頂層塊。語法是「給這個 suggester 起個名字 + 輸入前綴 + 指定 completion 字段」:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "suggest": { "movie-suggest": { "prefix": "the", "completion": { "field": "suggest" } } } }'
{
  "suggest" : {
    "movie-suggest" : [
      {
        "text" : "the",
        "offset" : 0,
        "length" : 3,
        "options" : [
          { "text" : "The Martian", "_index" : "movies_suggest", "_id" : "1" },
          { "text" : "The Matrix", "_index" : "movies_suggest", "_id" : "2" }
        ]
      }
    ]
  }
}

三個觀察點,是整篇的核心:

  • 返回結構分兩層。suggest 下面先是你起的名字(movie-suggest),它是陣列;陣列裡每個元素有 text(你輸入的前綴)和 options(補全結果列表)。補全的候選在 options[].text 裡。
  • 只匹配「開頭」。搜 the,補全出 The Martian、The Matrix 這兩個以 The 開頭的標題;Gone with the Wind、Let the Wind Rise 雖然也含 the,但 the 在中間,不補全出來。這就是「前綴 suggester」的邊界——見常見問題第 2 項。
  • 補全列表是灌進去的,不是現算的。你在第一步灌了 5 個標題進 suggest 字段,這裡才能補全;text/keyword 字段跑 suggest 是空的。

再搜一個開頭的例子印證「前綴」邊界:搜 wi:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "suggest": { "movie-suggest": { "prefix": "wi", "completion": { "field": "suggest" } } } }'

只補全出 Windmill Valley(windmill 以 wi 開頭);Gone with the Wind、Let the Wind Rise 裡的 wind 不匹配,因為它們的開頭是 gone、let。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "suggest": { "movie-suggest": { "prefix": "the", "completion": { "field": "suggest" } } } }' \
  | grep -o '"text" : "The [A-Za-z]*"'

能 grep 到 The Martian、The Matrix 就通過。

第三步:size 控制補全條數 + _source 過濾返回字段

預設補全會返回一批候選,用 size 限制條數;再用 _source 只返回你要的字段(比如只要標題,不要別的):

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "_source": "title", "suggest": { "movie-suggest": { "prefix": "the", "completion": { "field": "suggest", "size": 3 } } } }'
{
  "suggest" : {
    "movie-suggest" : [
      {
        "text" : "the",
        "options" : [
          { "text" : "The Martian", "_id" : "1", "_source" : { "title" : "The Martian" } },
          { "text" : "The Matrix", "_id" : "2", "_source" : { "title" : "The Matrix" } }
        ]
      }
    ]
  }
}

兩個細節:

  • size 放在 completion 塊裡(不是 suggest 頂層),控制這個 suggester 返回幾條候選。
  • _source 放在請求頂層(和 suggest 平級),控制補全結果裡附帶哪些字段。_source: "title" 讓每個 option 多一個 _source,只帶 title。注意源過濾只在 _search 端點生效,_suggest 端點不支持。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "_source": "title", "suggest": { "movie-suggest": { "prefix": "the", "completion": { "field": "suggest", "size": 3 } } } }' \
  | grep -o '"_source" : {'

能 grep 到 "_source" : { 就通過。

第四步:fuzzy 模糊補全——拼錯也能補全

用戶把 the 打成 teh,預設補全不到。開 fuzzy 做容錯:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "suggest": { "movie-suggest": { "prefix": "teh", "completion": { "field": "suggest", "size": 3, "fuzzy": { "fuzziness": "AUTO" } } } } }'
{
  "suggest" : {
    "movie-suggest" : [
      {
        "text" : "teh",
        "options" : [
          { "text" : "The Martian" },
          { "text" : "The Matrix" }
        ]
      }
    ]
  }
}

teh 拼錯了一個字母,fuzzy 用編輯距離把它糾正到 the,照樣補全出 The Martian、The Matrix。fuzziness: "AUTO" 是自適應容錯;想用全預設,直接寫 "fuzzy": {} 或 "fuzzy": true 即可。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_suggest/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "suggest": { "movie-suggest": { "prefix": "teh", "completion": { "field": "suggest", "fuzzy": { "fuzziness": "AUTO" } } } } }' \
  | grep -o '"text" : "The [A-Za-z]*"'

拼錯的 teh 也能 grep 到 The Martian/The Matrix,就通過。

第五步:search_as_you_type 對比——補全中間詞

completion 只補全「開頭」,用戶輸入標題中間的詞(比如 wind 想補全 Gone with the Wind)它就無能為力。要補中間詞,用另一個字段類型 search_as_you_type——它自動建 n-gram 子字段,天然支持前綴 + 中綴匹配,而且不用提前灌補全列表。

建一個 movies_sayt 索引,title 用 search_as_you_type:

curl -sk -u admin:Devfix@2026 -X PUT "https://localhost:9200/movies_sayt?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "mappings": { "properties": { "title": { "type": "search_as_you_type" } } } }'
curl -sk -u admin:Devfix@2026 -X POST "https://localhost:9200/movies_sayt/_bulk?pretty" \
  -H "Content-Type: application/x-ndjson" \
  --data-binary '{"index":{"_id":"1"}}
{"title":"The Martian"}
{"index":{"_id":"2"}}
{"title":"Gone with the Wind"}
{"index":{"_id":"3"}}
{"title":"Windmill Valley"}
'

注意:search_as_you_type 走的是普通查詢(不是 suggest),用 multi_match 的 bool_prefix 類型、同時查 _2gram/_3gram 子字段:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_sayt/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "multi_match": { "query": "wind", "type": "bool_prefix", "fields": [ "title", "title._2gram", "title._3gram" ] } } }'
{
  "hits" : {
    "total" : { "value" : 2, "relation" : "eq" },
    "hits" : [
      { "_id" : "2", "_source" : { "title" : "Gone with the Wind" } },
      { "_id" : "3", "_source" : { "title" : "Windmill Valley" } }
    ]
  }
}

Gone with the Wind 補全出來了——wind 在中間也能匹配。這就是 search_as_you_type 和 completion 的分工:completion 快、省事、只前綴;search_as_you_type 能補中間詞、走普通查詢、但索引更大(n-gram 子字段佔空間)。二選一,看你要不要「中間詞補全」。

驗證命令:

curl -sk -u admin:Devfix@2026 -X GET "https://localhost:9200/movies_sayt/_search?pretty" \
  -H "Content-Type: application/json" \
  -d '{ "query": { "multi_match": { "query": "wind", "type": "bool_prefix", "fields": [ "title", "title._2gram", "title._3gram" ] } } }' \
  | grep -o '"value" : [0-9]*'

返回 "value" : 2(含 Gone with the Wind)就通過。

第六步:收場

兩個演示索引用完刪掉即可(不影響 movies):

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

核心概念

這張圖把自動補全的兩條路線擺清楚:completion 字段在索引期把補全列表灌進記憶體 FST,查詢走 suggest 端點 + prefix 前綴匹配,返回 options[];search_as_you_type 字段自動建 n-gram 子字段,走普通 _search 查詢,能做中綴匹配。兩者都解決「邊輸入邊補全」,但機制、代價、能力邊界不同。

OpenSearch 搜索建議與自動補全鉛筆手繪詳解圖:左側定義卡解釋什麼是 completion 字段,中間橫排卡片對應正文六步,底部詳解 completion 與 search_as_you_type 兩條路線的分工

概念 是什麼 關鍵點
completion 字段 存補全候選的專門字段類型 索引期建記憶體 FST,快但佔記憶體,要提前灌
suggest 端點 補全的查詢入口(頂層塊,與 query 平級) prefix + completion.field,返回 options[]
前綴匹配 completion 只匹配候選值「開頭」 搜 the 補全 The Martian,不補全 Gone with the Wind
search_as_you_type 另一種自動補全字段類型 n-gram 子字段,前綴 + 中綴,走普通查詢
fuzzy 拼寫容錯 fuzziness: AUTO,teh 能補全 the 開頭

要點回顧

  • 補全用 completion 字段 + suggest 端點——不是普通 text 字段 + prefix query;補全列表要在索引期灌進記憶體 FST。
  • completion 只做前綴匹配——只匹配候選值開頭,中間詞補全不到;要中綴匹配換 search_as_you_type。
  • suggest 返回結構分兩層——suggest.<名字>[].options[].text 才是補全候選,別在 text(輸入前綴)裡找結果。
  • 中文要顯式配分析器——completion 預設 simple 分析器不認中文,中文要換 standard 或 IK。

常見問題

傳了 suggest,options 是空陣列

現象:跑 suggest 返回 options: [],沒有補全結果。

原因:completion 補全要用專門的 completion 字段類型(text/keyword 字段不行)、補全列表要提前灌進索引、且只匹配文本開頭;字段類型不對、沒灌列表、或前綴不匹配都會空。

相關文章:OpenSearch 自動補全沒結果:completion 字段或 suggest 用法不對

搜中間詞補全不到

現象:輸入標題中間的詞(如 wind),補全不出 Gone with the Wind。

原因:completion 是前綴 suggester,只匹配文本開頭;要匹配中間詞(infix),用 search_as_you_type 字段或 n-gram 分析器。

相關文章:OpenSearch 補全中間詞沒結果:completion 只做前綴匹配

中文標題補全不到

現象:中文標題做補全,輸入中文前綴沒結果或結果不對。

原因:completion 預設 simple 分析器按非字母切分,中文被當成整串或切碎,前綴匹配失效;中文要顯式用 standard 或 IK 分析器。

相關文章:OpenSearch 中文自動補全失敗:completion 預設 simple 分析器不認中文

下一步

補全之後,命中怎麼排

補全解決「幫用戶把詞打完」,打完搜出結果誰排前面是相關性的事。BM25 打分、explain 溯源、boost 調權一起看。

從零跑通 OpenSearch 相關性排序

中文補全要配分析器

中文詞級補全、拼音補全都依賴分析器。IK 插件、自定義詞典、分詞邊界,是中文搜索的基石。

從零跑通 OpenSearch 中文搜索

補全候選從哪來:詞的處理

補全的候選詞、命中詞,都和「分詞 + 同義詞 + 停用詞」相關。分析器配得好,補全和高亮才都準。

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

卡在哪一步了?

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

貢獻一條解法