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:要查询的字段
- article-suggester:这次查询的名称,自定义
推荐结果
上面的查询返回结果如下:
{
"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”