ES实现自动补全

2021-02-22 14:38:04 浏览数 (1)

什么是自动补全

随用户输入,给与提示信息,如下图:

ES实现原理

⽤户每输⼊⼀个 字符,就需要即时发送⼀个查询请求到后段查找匹配项。

对性能要求⽐较苛刻。Elasticsearch 采⽤FST,FST 会被 ES 整个加载进内存, 速度很快。

实现方式:

Completion Suggester 实现

1.定义 Mapping,使⽤ “completion” type

2.索引数据

3.运⾏ “suggest” 查询,得到搜索建议。

例子

1.定义 Mapping,使⽤ “completion” type

代码语言:javascript复制
DELETE inputcompletion
# 设置mapping
PUT inputcompletion
{
  "mappings": {
    "_doc": {
        "properties": {
          "input_completion": {
            "type": "completion"
          }
        }
      }
  }
}

2.索引数据

代码语言:javascript复制
POST inputcompletion/_doc/_bulk
{ "index" : { } }
{ "input_completion": "elasticsearch 教程"}
{ "index" : { } }
{ "input_completion": "elasticsearch api 中文"}
{ "index" : { } }
{ "input_completion": "elasticsearch"}

3.运⾏ “suggest” 查询,得到搜索建议。

代码语言:javascript复制
POST inputcompletion/_doc/_search?pretty
{
  "size": 0,
  "suggest": {
    "input-suggester": {
      "prefix": "ela",
      "completion": {
        "field": "input_completion"
      }
    }
  }
}

结果

代码语言:javascript复制
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "suggest": {
    "input-suggester": [
      {
        "text": "ela",
        "offset": 0,
        "length": 3,
        "options": [
          {
            "text": "elasticsearch",
            "_index": "inputcompletion",
            "_type": "_doc",
            "_id": "ezqix3cBMB6-gr9-ORLt",
            "_score": 1,
            "_source": {
              "input_completion": "elasticsearch"
            }
          },
          {
            "text": "elasticsearch api 中文",
            "_index": "inputcompletion",
            "_type": "_doc",
            "_id": "ejqix3cBMB6-gr9-ORLt",
            "_score": 1,
            "_source": {
              "input_completion": "elasticsearch api 中文"
            }
          },
          {
            "text": "elasticsearch 教程",
            "_index": "inputcompletion",
            "_type": "_doc",
            "_id": "eTqix3cBMB6-gr9-ORLt",
            "_score": 1,
            "_source": {
              "input_completion": "elasticsearch 教程"
            }
          }
        ]
      }
    ]
  }
}

Context Suggester带上下文的推荐

例如:再手机品类下搜索,小米提示小米手机,再食品下,提示真空小米

定义两种类型的 Context

Category – 任意的字符串

Geo – 地理位置信息

实现 Context Suggester 的具体步骤

1.定制⼀个 Mapping 索引数据

2.并且为每个⽂档加⼊ Context 信息

3.结合 Context 进⾏ Suggestion 查询

例子

1. 定制⼀个 Mapping 索引数据

代码语言:javascript复制
DELETE inputcompletion

PUT inputcompletion
{
  "mappings": {
    "_doc": {
        "properties": {
          "input_completion": {
            "type": "completion",
            "contexts":[{
              "type":"category",
              "name":"goods_category"
            }]
          }
        }
      }
  }
}

2.并且为每个⽂档加⼊ Context 信息

代码语言:javascript复制
POST inputcompletion/_doc
{
  "comment":"小米手机",
  "input_completion":{
    "input":["小米"],
    "contexts":{
      "goods_category":"手机"
    }
  }
}

POST inputcompletion/_doc
{
  "comment":"真空小米",
  "input_completion":{
    "input":["小米"],
    "contexts":{
      "goods_category":"食品"
    }
  }
}

3. 结合 Context 进⾏ Suggestion 查询

代码语言:javascript复制
POST inputcompletion/_search
{
  "suggest": {
    "MY_SUGGESTION": {
      "prefix": "小米",
      "completion":{
        "field":"input_completion",
        "contexts":{
          "goods_category":"食品"
        }
      }
    }
  }
}

结果

代码语言:javascript复制
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },

  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },

  "suggest": {

    "MY_SUGGESTION": [
      {
        "text": "小米",
        "offset": 0,
        "length": 2,
        "options": [
          {
            "text": "小米",
            "_index": "inputcompletion",
            "_type": "_doc",
            "_id": "bjqgx3cBMB6-gr9-vBL2",
            "_score": 1,
            "_source": {
              "comment": "真空小米",
              "input_completion": {
                "input": [
                  "小米"
                ],
                "contexts": {
                  "goods_category": "食品"
                }
              }
            },
            "contexts": {
              "goods_category": [
                "食品"
              ]
            }
          }
        ]
      }
    ]
  }
}

参考

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-suggesters-completion.html

https://www.elastic.co/guide/en/elasticsearch/reference/7.1/suggester-context.html

0 人点赞