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 同义词与停用词

卡在哪一步了?

从零跑通的过程中撞到报错?把命令和输出记下来,帮助下一个刚起步的人。

贡献一条解法