首页 > 解决方案 > Elasticsearch 自动完成拼写错误

问题描述

我正在尝试创建具有与Apollo graphql类似的功能的自动完成功能。基本上它:

  1. 在我输入时搜索
  2. 处理错别字
  3. 最重视标题,然后是子标题,最后是内容

虽然在 Apollo graphql doc 中这个功能是由 algolia 提供的,但我很确定我可以用 elasticsearch 构建它。

我从Search-as-you-type 字段类型开始,并轻松使其按如下方式工作。

映射:

PUT /article
{
  "mappings": {
    "properties": {
      "title": {
        "type": "search_as_you_type"
      }
    }
  }
}

一些虚拟数据:

PUT /article/_bulk
{ "index": {"_id": "1"} }
{   "title": "Authentication and authorization", "subtitle": "Putting authenticated user info on the context" }

并搜索:

GET /article/_search
{
    "query": {
        "multi_match": {
            "query": "auth",
            "type": "bool_prefix",
            "fuzziness" : "AUTO",
            "prefix_length" : 2,
            "fields": [
                "title",
                "title._2gram",
                "title._3gram",
                "title._index_prefix"
            ]
        }
    }
}

现在我能够得到以下信息:

a -> Authentication and authorization
au -> Authentication and authorization
aut -> Authentication and authorization
...

但是当我把一个词拼错到autenticationES 时,什么也没有返回。

经过一些研究,我发现它fuzziness不适用于bool_prefix. 看:

那么请有其他方法来实现这种期望的行为吗?还是elasticsearch技术只是无法实现这个功能?

标签: elasticsearch

解决方案


推荐阅读