美文网首页
4.12-自动补全与基于上下文的提示

4.12-自动补全与基于上下文的提示

作者: 落日彼岸 | 来源:发表于2020-04-04 22:51 被阅读0次

    The Completion Suggester

    • Completion Suggester 提供了“⾃动完成” (Auto Complete) 的功能。⽤户每输⼊⼀个 字符,就需要即时发送⼀个查询请求到后段查找匹配项

    • 对性能要求⽐较苛刻。Elasticsearch 采⽤了不同的数据结构,并⾮通过倒排索引来完成。
      ⽽是将 Analyze 的数据编码成 FST 和索引⼀起存放。FST 会被 ES 整个加载进内存, 速度很快

    • FST 只能⽤于前缀查找

    使⽤ Completion Suggester 的⼀些步骤

    • 定义 Mapping,使⽤ “completion” type

    • 索引数据

    • 运⾏ “suggest” 查询,得到搜索建议

    PUT articles
    {
      "mappings": {
        "properties": {
          "title_completion":{
            "type": "completion"
          }
        }
      }
    }
    

    索引数据

    POST articles/_bulk
    { "index" : { } }
    { "title_completion": "lucene is very cool"}
    { "index" : { } }
    { "title_completion": "Elasticsearch builds on top of lucene"}
    { "index" : { } }
    { "title_completion": "Elasticsearch rocks"}
    { "index" : { } }
    { "title_completion": "elastic is the company behind ELK stack"}
    { "index" : { } }
    { "title_completion": "Elk stack rocks"}
    { "index" : {} }
    

    搜索数据

    POST articles/_search?pretty
    {
      "size": 0,
      "suggest": {
        "article-suggester": {
          "prefix": "elk ",
          "completion": {
            "field": "title_completion"
          }
        }
      }
    }
    
    res

    什么是 Context Suggester

    • Completion Suggester 的扩展

    • 可以在搜索中加⼊更多的上下⽂信息,例如,输⼊ “star”

      • 咖啡相关:建议 “Starbucks”

      • 电影相关:”star wars”

    实现 Context Suggester

    • 可以定义两种类型的 Context

      • Category – 任意的字符串

      • Geo – 地理位置信息

    • 实现 Context Suggester 的具体步骤

      • 定制⼀个 Mapping

      • 索引数据,并且为每个⽂档加⼊ Context 信息

      • 结合 Context 进⾏ Suggestion 查询

    定义 Mapping

    • 增加 Contexts

      • Type

      • name

    PUT comments/_mapping
    {
      "properties": {
        "comment_autocomplete":{
          "type": "completion",
          "contexts":[{
            "type":"category",
            "name":"comment_category"
          }]
        }
      }
    }
    

    索引数据

    image.png

    不同的上下⽂,⾃动提示

    POST comments/_search
    {
      "suggest": {
        "MY_SUGGESTION": {
          "prefix": "sta",
          "completion":{
            "field":"comment_autocomplete",
            "contexts":{
              "comment_category":"coffee"
            }
          }
        }
      }
    }
    
    image.png

    精准度和召回率

    • 精准度

      • Completion > Phrase > Term
    • 召回率

      • Term > Phrase > Completion
    • 性能

      • Completion > Phrase > Term

    本节知识点回顾

    • Completion Suggester,对性能要求⽐较苛刻。采⽤了不同的数据结构,并⾮通过倒排
      索引来完成。⽽是将 Analyze 的数据编码成 FST 和索引⼀起存放。FST 会被 ES 整个 加载进内存,速度很快

    • 需要设置特定的 Mapping

    • Context Completion Suggester ⽀持结合不同的上下⽂,给出推荐

    课程demo

    DELETE articles
    PUT articles
    {
      "mappings": {
        "properties": {
          "title_completion":{
            "type": "completion"
          }
        }
      }
    }
    
    POST articles/_bulk
    { "index" : { } }
    { "title_completion": "lucene is very cool"}
    { "index" : { } }
    { "title_completion": "Elasticsearch builds on top of lucene"}
    { "index" : { } }
    { "title_completion": "Elasticsearch rocks"}
    { "index" : { } }
    { "title_completion": "elastic is the company behind ELK stack"}
    { "index" : { } }
    { "title_completion": "Elk stack rocks"}
    { "index" : {} }
    
    
    POST articles/_search?pretty
    {
      "size": 0,
      "suggest": {
        "article-suggester": {
          "prefix": "elk ",
          "completion": {
            "field": "title_completion"
          }
        }
      }
    }
    
    
    DELETE comments
    PUT comments
    PUT comments/_mapping
    {
      "properties": {
        "comment_autocomplete":{
          "type": "completion",
          "contexts":[{
            "type":"category",
            "name":"comment_category"
          }]
        }
      }
    }
    
    POST comments/_doc
    {
      "comment":"I love the star war movies",
      "comment_autocomplete":{
        "input":["star wars"],
        "contexts":{
          "comment_category":"movies"
        }
      }
    }
    
    POST comments/_doc
    {
      "comment":"Where can I find a Starbucks",
      "comment_autocomplete":{
        "input":["starbucks"],
        "contexts":{
          "comment_category":"coffee"
        }
      }
    }
    
    
    POST comments/_search
    {
      "suggest": {
        "MY_SUGGESTION": {
          "prefix": "sta",
          "completion":{
            "field":"comment_autocomplete",
            "contexts":{
              "comment_category":"coffee"
            }
          }
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:4.12-自动补全与基于上下文的提示

          本文链接:https://www.haomeiwen.com/subject/sfqephtx.html