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

卡在哪一步了?

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

贡献一条解法