即使遗漏首字母,Elasticsearch 拼写检查建议

Mak*_*sim 5 spell-checking elasticsearch

我创建了一个这样的索引:

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" }
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

当我创建文档时:

curl --location --request POST 'http://127.0.0.1:9200/test/_doc/' \
--header 'Content-Type: application/json' \
--data-raw '{ "word":"organic" }'
Run Code Online (Sandbox Code Playgroud)

最后,用一个故意拼错的词进行搜索:

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"
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

“organic”这个词丢失了第一个字母 - ES 从来没有为这种拼写错误提供建议选项(对于任何其他拼写错误 - 'orgnic'、'oragnc' 和 'organi' 绝对适用)。我错过了什么?

Ema*_*lev 6

发生这种情况是因为prefix_length参数: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
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)