Elasticsearch 自动补全与机遇上下文的提示

本文详细介绍了Elasticsearch中的Completion Suggester和Context Suggester。Completion Suggester提供实时的自动补全功能,通过FST(Finite State Transducers)实现高效查询。文章通过实例展示了如何配置Mapping、索引数据和执行搜索建议查询。而Context Suggester作为Completion Suggester的扩展,允许在搜索建议中引入上下文信息,如在‘star’查询中区分咖啡和电影相关的结果。文章还演示了如何定义Context Mapping,索引带有上下文信息的数据,并执行上下文敏感的建议查询。


1. The Completion Suggester

Completion Suggester 提供了 “自动完成”(Auto Complete)的功能。用户每输入一个字符,就需要即时发送一个插叙请求到后端查询匹配项

  • 对性能要求比较苛刻。ES 采用了不同的数据结构,并非通过倒排索引来完成。而是将 Analyze 的数据编码成 FST和索引一起存放。FST 会被 ES 整个加载进内容,速度很快
  • FST 只能用于前缀查找

2. 使用 Completion Suggester 一些步骤

  • 定义 Mapping,使用 “completion” type
  • 索引数据
  • 运行 “suggest” 查询,得到搜索建议
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"
      }
    }
  }
}

返回输出:
{
  "took" : 0,
  "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" : "elk ",
        "offset" : 0,
        "length" : 4,
        "options" : [
          {
            "text" : "Elk stack rocks",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "ITkIzncBVWBR55DpjKhv",
            "_score" : 1.0,
            "_source" : {
              "title_completion" : "Elk stack rocks"
            }
          }
        ]
      }
    ]
  }
}

3. 什么是 Context Suggester

Completion Suggester 的扩展
可以在搜索中加入耕读偶读上下文信息,例如,输入 “star”

  • 咖啡相关:starbucks
  • 电影相关:star wars

4. 实现 Context Suggester

可以定义两种类型的 Context

  • Category - 任意的字符串
  • Geo - 地理信息位置

实现 Context Suggester

  • 定制一个 Mapping
  • 索引数据,并且为每个文档加入 Conetxt 信息
  • 结合 Context 进行 Suggestion 查询
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"
        }
      }
    }
  }
}

返回输出:
{
  "took" : 958,
  "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" : "4DkSzncBVWBR55DpQ6rd",
            "_score" : 1.0,
            "_source" : {
              "comment" : "Where can I find a Starbucks",
              "comment_autocomplete" : {
                "input" : [
                  "starbucks"
                ],
                "contexts" : {
                  "comment_category" : "coffee"
                }
              }
            },
            "contexts" : {
              "comment_category" : [
                "coffee"
              ]
            }
          }
        ]
      }
    ]
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghostwritten

口渴,请赏一杯下午茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值