ES--------自动补全和提示

ES的推荐功能(Suggester)包含三种不同方式,不过用的最多的,还是Completion模式,实现自动补全基于上下文的提示功能。
相关文档

准备数据

理想中,自动补全功能需要在用户键入一个字符时,尽可能快速的给用户返回提示信息。因此自动补全很注重查询的速度。为了提高suggester的速度,相关的数据必须在内存中缓存起来,数据的类型也不再是普通类型,而是completion类型。

首先我们要定义一个索引库,并设置用于自动补全的字段为completion类型。

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

创建一个名为articles的索引库,并且有一个字段为suggestion,类型是completion

然后批量插入一些数据:

POST articles/_bulk
{ "index" : { } }
{ "suggestion": ["lucene", "is", "very", "cool"]}
{ "index" : { } }
{ "suggestion": ["Elasticsearch", "builds", "on", "lucene"]}
{ "index" : { } }
{ "suggestion": ["Elasticsearch", "rocks"]}
{ "index" : { } }
{ "suggestion": ["elastic", "is", "the", "company", "behind", "ELK"]}
{ "index" : { } }
{ "suggestion": ["Elk", "stack", "rocks"]}

在一个文档中,completion类型的字段,其值可以有多个,它的每一个值都可以成为自动补全的推荐结果。

查询推荐值

如果我们要为用户输入的字符补全完整字符,可以向ES发起请求指定要在哪个completion类型的字段上进行查询,示例:

POST articles/_search
{
  "suggest": {
    "article-suggester": {
      "prefix": "el ",
      "completion": {
        "field": "suggestion",
        "size": 10
      }
    }
  }
}

参数说明:

  • suggest:代表接下来的查询是一个suggest类型的查询
    • article-suggester:这次查询的名称,自定义
      • prefix:用来补全的词语前缀,本例中搜索以 el开头的内容
      • completion:代表是completion类型的suggest,其它类型还有:Term、Phrase
        • field:要查询的字段

推荐结果

上面的查询返回结果如下:

{
  "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" : "el",
        "offset" : 0,
        "length" : 2,
        "options" : [
          {
            "text" : "Elasticsearch",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "ZvxiWHABGZL7VTV2dPvC",
            "_score" : 1.0,
            "_source" : {
              "suggestion" : [
                "Elasticsearch",
                "builds",
                "on",
                "lucene"
              ]
            }
          },
          {
            "text" : "Elasticsearch",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "Z_xiWHABGZL7VTV2dPvC",
            "_score" : 1.0,
            "_source" : {
              "suggestion" : [
                "Elasticsearch",
                "rocks"
              ]
            }
          },
          {
            "text" : "Elk",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "afxiWHABGZL7VTV2dPvC",
            "_score" : 1.0,
            "_source" : {
              "suggestion" : [
                "Elk",
                "stack",
                "rocks"
              ]
            }
          },
          {
            "text" : "elastic",
            "_index" : "articles",
            "_type" : "_doc",
            "_id" : "aPxiWHABGZL7VTV2dPvC",
            "_score" : 1.0,
            "_source" : {
              "suggestion" : [
                "elastic",
                "is",
                "the",
                "company",
                "behind",
                "ELK"
              ]
            }
          }
        ]
      }
    ]
  }
}

返回结果中的options数组就是推荐的结果,其中text是推荐的文本,_source是文档原始数据。

本例中有四条推荐结果:

  • Elasticsearch,文档的内容是:“Elasticsearch”, “builds”, “on”, “lucene”
  • Elasticsearch,文档的内容是:“Elasticsearch”, “rocks”
  • ELK,文档内容是:“Elk”, “stack”, “rocks”
  • elastic,文档内容是:“elastic is the company behind ELK stack”
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值