如何使用 ElasticSearch 搜索复合词?

How to search for a composite word using ElasticSearch?

我使用 Elasticsearch 有一段时间了,但我似乎找不到解决这个特定问题的分析器: 我有一个包含以下文本的文档:

"The Harry Potter and the Sorcerer Stone is a great book".

如果用户键入 "harrypotter"(不带空格),我需要能够将该文档显示为搜索结果。可能吗? 我查看了 Ngram,但它似乎无法解决这种情况,因为使用 Ngram,我必须将整个文档存储为:

"TheHarryPotterandtheSorcererStoneisagreatbook".

这不是我想要的,因为如果用户搜索 "harry potter stone",文档也不会显示。

您可以在 ngram 配置中使用 "token_chars": [ "letter", "digit", "whitespace"] 来索引空格,这将匹配您的查询 "harry potter stone" 和 "harrypotter"

参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html

编辑: 您还可以在内部字段中索引 'ngram' 版本,然后查询正常分析的字段和 ngram 字段(包装在 bool 查询中)