使用同义词搜索
Elastic Stack Serverless
同义词是具有相同或相似含义的单词或短语。它们是搜索的重要方面,因为它们可以改善搜索体验并扩大搜索结果的范围。
同义词允许您
- 提高搜索相关性,通过查找使用不同术语表达相同概念的相关文档。
- 使特定领域的词汇更易于用户使用,允许用户使用他们更熟悉的搜索词。
- 定义常见的拼写错误和笔误,以透明地处理常见错误。
同义词使用同义词集进行分组。您可以拥有任意数量的同义词集。
为了在 Elasticsearch 中使用同义词集,您需要
您的同义词集需要存储在 Elasticsearch 中,以便您的分析器可以引用它们。有三种方法可以存储您的同义词集:
您可以使用 同义词 API 来管理同义词集。这是最灵活的方法,因为它允许动态定义和修改同义词集。
您的同义词集中的更改将自动重新加载相关的分析器。
您可以将同义词集存储在文件中。
同义词集文件需要上传到您的所有集群节点,并放置在 Elasticsearch 发行版的配置目录中。如果您使用的是 Elastic Cloud Hosted,可以使用 自定义包 上传同义词文件。
一个同义词文件示例
# Blank lines and lines starting with pound are comments.
# Explicit mappings match any token sequence on the left hand side of "=>"
# and replace with all alternatives on the right hand side.
# These types of mappings ignore the expand parameter in the schema.
# Examples:
i-pod, i pod => ipod
sea biscuit, sea biscit => seabiscuit
# Equivalent synonyms may be separated with commas and give
# no explicit mapping. In this case the mapping behavior will
# be taken from the expand parameter in the token filter configuration.
# This allows the same synonym file to be used in different synonym handling strategies.
# Examples:
ipod, i-pod, i pod
foozball , foosball
universe , cosmos
lol, laughing out loud
# If expand==true in the synonym token filter configuration,
# "ipod, i-pod, i pod" is equivalent to the explicit mapping:
ipod, i-pod, i pod => ipod, i-pod, i pod
# If expand==false, "ipod, i-pod, i pod" is equivalent
# to the explicit mapping:
ipod, i-pod, i pod => ipod
# Multiple synonym mapping entries are merged.
foo => foo bar
foo => baz
# is equivalent to
foo => foo bar, baz
要更新现有的同义词集,请将新文件上传到您的集群。同义词集文件必须在每个集群节点上保持同步。
当同义词集更新时,使用它的搜索分析器需要使用 重新加载搜索分析器 API 进行刷新。
这种手动同步和重新加载使得这种方法不如使用 同义词 API 灵活。
您可以通过将同义词直接添加到令牌过滤器定义中来测试您的同义词。
不建议在生产环境中使用内联同义词。大量的内联同义词会不必要地增加集群大小,并可能导致性能问题。
创建同义词集后,您可以开始配置令牌过滤器和分析器以使用它们。
同义词集必须先存在才能添加到索引中。如果创建的索引引用了不存在的同义词集,该索引将处于部分创建且无法操作的状态。解决此问题的唯一方法是确保同义词集存在,然后删除并重新创建索引,或关闭并重新打开索引。
无效的同义词规则可能导致应用分析器更改时出错。对于可重新加载的分析器,这会阻止重新加载和应用更改。您必须纠正同义词规则中的错误并重新加载分析器。
具有无效同义词规则的索引无法重新打开,导致其在以下情况下无法操作:
- 包含该索引的节点启动
- 索引从关闭状态打开
- 发生节点重启(这将重新打开分配的节点分片)
Elasticsearch 在 分析过程 中使用同义词。您可以使用两种类型的 令牌过滤器 来包含同义词:
请查阅每个同义词令牌过滤器的文档,了解配置详细信息和将其添加到分析器的说明。
您可以在不修改索引设置的情况下测试分析器配置。使用 分析 API 来测试您的分析器链。
GET /_analyze {
"tokenizer": "standard",
"filter" : [
"lowercase",
{
"type": "synonym_graph",
"synonyms": ["pc => personal computer", "computer, pc, laptop"]
}
],
"text" : "Check how PC synonyms work"
}
分析器可以在 索引时或搜索时 应用。
您需要决定何时应用您的同义词。
- 索引时:在文档被索引到 Elasticsearch 时应用同义词。这是一个不太灵活的选择,因为更改同义词需要 重新索引。
- 搜索时:在执行搜索时应用同义词。这是一种更灵活的方法,不需要重新索引。如果令牌过滤器配置为
"updateable": true,当您更改同义词时,搜索分析器可以 重新加载。
使用 同义词 API 创建的同义词集只能在搜索时使用。
您可以将包含同义词集的分析器指定为 搜索时分析器 或 索引时分析器。
以下示例在索引映射的 title 字段中将 my_analyzer 添加为搜索时分析器。
{
"mappings": {
"properties": {
"title": {
"type": "text",
"search_analyzer": "my_analyzer"
}
}
},
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "whitespace",
"filter": [
"synonyms_filter"
]
}
},
"filter": {
"synonyms_filter": {
"type": "synonym",
"synonyms_path": "analysis/synonym-set.txt",
"updateable": true
}
}
}
}
}