美文网首页
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-自动补全与基于上下文的提示

    The Completion Suggester Completion Suggester 提供了“⾃动完成” (...

  • 2020-10-20 IDEA超级实用的代码智能提示 Codot

    Codota 这个插件用于智能代码补全,它基于数百万Java程序,能够根据程序上下文提示补全代码。相比于IDEA自...

  • IDEA 快捷键

    Alt+Enter 上下文补全Ctrl+Shift+Enter 补全语句(使用Tab选择自动补全的代码是替换,E...

  • 使用ElasticSearch实现检索词补全功能

    需求说明系统是基于ES进行检索,在输入检索内容时,需要自动补全后续内容,例如输入【危害】,会自动提示【危害国家安全...

  • centos下安装zsh

    zsh zsh可以提供命令参数自动补全和提示.和传统的TAB补全不同,传统的TAB补全并不会提示命令参数。让命令不...

  • 11.1PHPstorm的日常用快捷键收集

    关于自动提示: 1、提示函数参数:Ctrl+P2、CTRL+J 自动代码提示,自动补全 3、上下移动代码块Ctrl...

  • redis实现自动补全

    忘了redis从哪个版本开启,能够根据输入的部分命令前缀给出提示,即自动补全。接下来笔者介绍基于redis实现这个...

  • JavaScript

    自动补全在浏览器开发者工具的console中,输入代码有自动补全功能。有补全提示时按方向右键 —> 或者制表符 ...

  • atom 的js自动补全

    atom的js自动补全不像sublime的js自动不全,按部就班、从左到右自动提示;而是输入关键字提示。举个例子:...

  • IDLE编辑器快捷键一些技巧

    IDLE编辑器快捷键自动补全代码 Alt+/(查找编辑器内已经写过的代码来补全)补全提示 ...

网友评论

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

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