ES 笔记二十七:自动补全与机遇上下文的提示

The Completion Suggester

  • Completion Suggester 提供了“自动完成”(Auto Complete)的功能。用户每输入一个字符,就需要即时发送一个插叙请求到后端查询匹配项
    • 对性能要求比较苛刻。ES采用了不同的数据结构,并非通过倒排索引来完成。而是将Analyze 的数据编码成FST和索引一起存放。FST会被ES整个加载进内容,速度很快
    • FST只能用于前缀查找

使用Completion Suggester 一些步骤

  • 定义Mapping,使用“completion” type
  • 索引数据
  • 运行“suggest”查询,得到搜索建议
//定义mapping
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-suggest": {
      "prefix": "e", //查询字段 
      "completion": {
        "field": "title_completion"
      }
    }
  }
}

什么是Context Suggester

  • Completion Suggester 的扩展
  • 可以在搜索中加入耕读偶读上下文信息,例如,输入“star”
    • 咖啡相关:starbucks
    • 电影相关:star wars

实现 Context Suggester

  • 可以定义两种类型的Context
    • Category - 任意的字符串
    • Geo - 地理信息位置
  • 实现Context Suggester
    • 定制一个Mapping
    • 索引数据,并且为每个文档加入Conetxt信息
    • 结合Context 进行Suggestion 查询
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":"movies" 
        //  "comment_category":"coffee"  
        }
      }
    }
  }
}

精确度 和召回率

  • 精准度
    • Completion > Phrase > Term
  • 召回率
    • Term > Phrase > Completion
  • 性能
    • Completion > Phrase > Term
本作品采用《CC 协议》,转载必须注明作者和本文链接

快乐就是解决一个又一个的问题!

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!