我创建一个这样的索引:
curl --location --request PUT 'http://127.0.0.1:9200/test/' \
--header 'Content-Type: application/json' \
--data-raw '{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"word" : { "type" : "text" }
}
}
}'
当我创建文档时:
curl --location --request POST 'http://127.0.0.1:9200/test/_doc/' \
--header 'Content-Type: application/json' \
--data-raw '{ "word":"organic" }'
最后,使用故意拼写错误的单词进行搜索:
curl --location --request POST 'http://127.0.0.1:9200/test/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"suggest": {
"001" : {
"text" : "rganic",
"term" : {
"field" : "word"
}
}
}
}'
“organic”一词丢失了第一个字母 - ES 从未为此类拼写错误提供建议选项(对于任何其他拼写错误 - 'orgnic'、'oragnc' 和 'organi' 绝对没问题)。我缺少什么?
发生这种情况是因为prefix_length
范围:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html。默认值为 1,即术语开头至少 1 个字母必须匹配。您可以设置prefix_length
为 0 但这会对性能产生影响。只有您的硬件、您的设置和数据集才能准确地向您展示这些在您的案例中的实际情况,即尝试一下:)。但是,请小心 - Elasticsearch 和 Lucene 开发人员将默认值设置为 1 是有原因的。
这是一个查询,在我执行您的设置步骤后,它会返回您在 Elasticsearch 7.4.0 上需要的建议结果。
curl --location --request POST 'http://127.0.0.1:9200/test/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"suggest": {
"001" : {
"text" : "rganic",
"term" : {
"field" : "word",
"prefix_length": 0
}
}
}
}'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)