使用同义词进行搜索
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灵活。
您可以通过将同义词直接内联添加到您的 token 过滤器定义中来测试您的同义词。
不建议将内联同义词用于生产环境。 大量的内联同义词会不必要地增加集群大小,并可能导致性能问题。
创建同义词集后,您可以开始配置您的 token 过滤器和分析器以使用它们。
同义词集必须存在才能添加到索引中。 如果创建索引时引用了不存在的同义词集,则索引将保持在部分创建且无法操作的状态。 摆脱这种情况的唯一方法是确保同义词集存在,然后删除并重新创建索引,或关闭并重新打开索引。
应用分析器更改时,无效的同义词规则可能会导致错误。 对于可重新加载的分析器,这会阻止重新加载和应用更改。 您必须更正同义词规则中的错误并重新加载分析器。
具有无效同义词规则的索引无法重新打开,从而使其在以下情况下无法操作
- 包含索引的节点启动
- 索引从关闭状态打开
- 发生节点重启(这将重新打开节点分配的分片)
Elasticsearch 使用同义词作为分析过程的一部分。 您可以使用两种类型的token 过滤器来包含同义词
查看每个同义词 token 过滤器文档,了解配置详细信息以及将其添加到分析器的说明。
您可以在不修改索引设置的情况下测试分析器配置。 使用analyze API来测试您的分析器链
GET /_analyze
{
"tokenizer": "standard",
"filter" : [
"lowercase",
{
"type": "synonym_graph",
"synonyms": ["pc => personal computer", "computer, pc, laptop"]
}
],
"text" : "Check how PC synonyms work"
}
分析器可以在索引时或搜索时应用。
您需要决定何时应用您的同义词
- 索引时:同义词在文档索引到 Elasticsearch 时应用。 这是一种不太灵活的替代方法,因为对同义词的更改需要重新索引。
- 搜索时:同义词在执行搜索时应用。 这是一种更灵活的方法,不需要重新索引。 如果 token 过滤器配置为
"updateable": true
,则当您更改同义词时,可以重新加载搜索分析器。
使用同义词 API创建的同义词集只能在搜索时使用。
您可以将包含同义词集的分析器指定为搜索时分析器或索引时分析器。
以下示例将 my_analyzer
作为搜索分析器添加到索引映射中的 title
字段
{
"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
}
}
}
}
}