ES實現自動補全

什麼是自動補全

隨用戶輸入,給與提示信息,如下圖:

ES實現原理

⽤戶每輸⼊⼀個 字符,就需要即時發送⼀個查詢請求到後段查找匹配項

對性能要求⽐較苛刻。Elasticsearch 採⽤FST,FST 會被 ES 整個加載進內存, 速度很快

實現方式:

Completion Suggester 實現

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

2.索引數據

3.運⾏ “suggest” 查詢,得到搜索建議

例子

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

DELETE inputcompletion
# 設置mapping
PUT inputcompletion
{
  "mappings": {
    "_doc": {
        "properties": {
          "input_completion": {
            "type": "completion"
          }
        }
      }
  }
}

2.索引數據

POST inputcompletion/_doc/_bulk
{ "index" : { } }
{ "input_completion": "elasticsearch 教程"}
{ "index" : { } }
{ "input_completion": "elasticsearch api 中文"}
{ "index" : { } }
{ "input_completion": "elasticsearch"}

3.運⾏ “suggest” 查詢,得到搜索建議

POST inputcompletion/_doc/_search?pretty
{
  "size": 0,
  "suggest": {
    "input-suggester": {
      "prefix": "ela",
      "completion": {
        "field": "input_completion"
      }
    }
  }
}

結果

{
  "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 索引數據

DELETE inputcompletion

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

2.並且爲每個⽂檔加⼊ Context 信息

POST inputcompletion/_doc
{
  "comment":"小米手機",
  "input_completion":{
    "input":["小米"],
    "contexts":{
      "goods_category":"手機"
    }
  }
}

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

3. 結合 Context 進⾏ Suggestion 查詢

POST inputcompletion/_search
{
  "suggest": {
    "MY_SUGGESTION": {
      "prefix": "小米",
      "completion":{
        "field":"input_completion",
        "contexts":{
          "goods_category":"食品"
        }
      }
    }
  }
}

結果

{
  "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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章