⾃动补全与基于上下⽂的提示(Completion Suggester)

The Completion Suggester
● Completion Suggester 提供了“⾃动完成” (Auto Complete) 的功能。⽤户每输⼊⼀个
字符,就需要即时发送⼀个查询请求到后段查找匹配项
● 对性能要求⽐较苛刻。Elasticsearch 采⽤了不同的数据结构,并⾮通过倒排索引来完成。
⽽是将 Analyze 的数据编码成 FST 和索引⼀起存放。FST 会被 ES 整个加载进内存,
速度很快
● FST 只能⽤于前缀查找

使⽤ Completion Suggester 

定义Mapping,使用”completion“type;

索引数据

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

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

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "article-suggester" : [
      {
        "text" : "el",
        "offset" : 0,
        "length" : 2,
        "options" : [
          {
            "text" : "Elasticsearch builds on top of lucene",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "iDmkSXEBQDCK-6_knSjv",
            "_score" : 1.0,
            "_source" : {
              "title_completion" : "Elasticsearch builds on top of lucene"
            }
          },
          {
            "text" : "Elasticsearch rocks",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "iTmkSXEBQDCK-6_knSjv",
            "_score" : 1.0,
            "_source" : {
              "title_completion" : "Elasticsearch rocks"
            }
          },
          {
            "text" : "Elk stack rocks",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "izmkSXEBQDCK-6_knSjv",
            "_score" : 1.0,
            "_source" : {
              "title_completion" : "Elk stack rocks"
            }
          },
          {
            "text" : "elastic is the company behind ELK stack",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "ijmkSXEBQDCK-6_knSjv",
            "_score" : 1.0,
            "_source" : {
              "title_completion" : "elastic is the company behind ELK stack"
            }
          }
        ]
      }
    ]
  }
}

什么是 Context Suggester
● Completion Suggester 的扩展
● 可以在搜索中加⼊更多的上下⽂信息,例如,输⼊ “star”
● 咖啡相关:建议 “Starbucks”
● 电影相关:”star wars”

实现 Context Suggester
● 可以定义两种类型的 Context
● Category – 任意的字符串
● Geo – 地理位置信息

● 实现 Context Suggester 的具体步骤
● 定制⼀个 Mapping
● 索引数据,并且为每个⽂档加⼊ Context 信息
● 结合 Context 进⾏ Suggestion 查询

● 增加 Contexts
● Type
● name

POST comments/_search
{
  "suggest": {
    "MY_SUGGESTION": {
      "prefix": "sta",
      "completion":{
        "field":"comment_autocomplete",
        "contexts":{
          "comment_category":"movies"
        }
      }
    }
  }
}

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "MY_SUGGESTION" : [
      {
        "text" : "sta",
        "offset" : 0,
        "length" : 3,
        "options" : [
          {
            "text" : "star wars",
            "_index" : "comments",
            "_type" : "_doc",
            "_id" : "jTmpSXEBQDCK-6_kyCj_",
            "_score" : 1.0,
            "_source" : {
              "comment" : "I love the star war movies",
              "comment_autocomplete" : {
                "input" : [
                  "star wars"
                ],
                "contexts" : {
                  "comment_category" : "movies"
                }
              }
            },
            "contexts" : {
              "comment_category" : [
                "movies"
              ]
            }
          }
        ]
      }
    ]
  }
}

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

{
  "took" : 654,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "MY_SUGGESTION" : [
      {
        "text" : "sta",
        "offset" : 0,
        "length" : 3,
        "options" : [
          {
            "text" : "starbucks",
            "_index" : "comments",
            "_type" : "_doc",
            "_id" : "jjmqSXEBQDCK-6_khCi1",
            "_score" : 1.0,
            "_source" : {
              "comment" : "Where can I find a Starbucks",
              "comment_autocomplete" : {
                "input" : [
                  "starbucks"
                ],
                "contexts" : {
                  "comment_category" : "coffee"
                }
              }
            },
            "contexts" : {
              "comment_category" : [
                "coffee"
              ]
            }
          },
          {
            "text" : "starbucks",
            "_index" : "comments",
            "_type" : "_doc",
            "_id" : "jzmsSXEBQDCK-6_kTyjm",
            "_score" : 1.0,
            "_source" : {
              "comment" : "Where can I find a Starbucks",
              "comment_autocomplete" : {
                "input" : [
                  "starbucks"
                ],
                "contexts" : {
                  "comment_category" : "coffee"
                }
              }
            },
            "contexts" : {
              "comment_category" : [
                "coffee"
              ]
            }
          }
        ]
      }
    ]
  }
}

精准度和召回率

● 精准度
● Completion > Phrase > Term
● 召回率
● Term > Phrase > Completion
● 性能
● Completion > Phrase > Term

● Completion Suggester,对性能要求⽐较苛刻。采⽤了不同的数据结构,并⾮通过倒
索引来完成。⽽是将 Analyze 的数据编码成 FST 和索引⼀起存放。FST 会被 ES 整个
加载进内存,速度很快
● 需要设置特定的 Mapping
● Context Completion Suggester ⽀持结合不同的上下⽂,给出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值