🔥本文简要🔥
同学们好!本篇文章主要是介绍Elasticsearch的自动补全功能,以及提供一个小案例给大家。
😄博主:程序员一灯,一个老菜鸟,哈哈哈哈
🚌一个人可以走的很快,一群人可以走的很远🇨🇳
- 🎉点赞➕评论➕收藏 ➕关注== 养成习惯(一键四连)📝
- 🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝
- 🙏作者水平有限,欢迎各位大佬指点,相互学习进步!😆
目录
🌵3.2.1、定义 Mapping,使⽤ “completion” type
🌵3.2.3、运⾏ “suggest” 查询,得到搜索建议。
🌵4.1.3、结合 Context 进⾏ Suggestion 查询
🍓一、什么是自动补全
随用户输入,给与提示信息,如下图:
🍓二、ES实现原理
⽤户每输⼊⼀个 字符,就需要即时发送⼀个查询请求到后段查找匹配项。
对性能要求⽐较苛刻。Elasticsearch 采⽤FST,FST 会被 ES 整个加载进内存, 速度很快。
🍓三、实现方式
🌈3.1、Completion Suggester 实现
- 定义 Mapping,使⽤ “completion” type
- 索引数据
- 运⾏ “suggest” 查询,得到搜索建议。
🌈3.2、例子
🌵3.2.1、定义 Mapping,使⽤ “completion” type
# 设置mapping
PUT inputcompletion
{
"mappings": {
"properties": {
"input_completion": {
"type": "completion"
}
}
}
}
🌵3.2.2、索引数据
POST inputcompletion/_doc/_bulk
{ "index" : { } }
{ "input_completion": "elasticsearch 教程"}
{ "index" : { } }
{ "input_completion": "elasticsearch api 中文"}
{ "index" : { } }
{ "input_completion": "elasticsearch"}
🌵3.2.3、运⾏ “suggest” 查询,得到搜索建议。
POST inputcompletion/_doc/_search?pretty
{
"size": 0,
"suggest": {
"input-suggester": {
"prefix": "ela",
"completion": {
"field": "input_completion"
}
}
}
}
🌵3.2.4、结果
"suggest" : {
"input-suggester" : [
{
"text" : "ela",
"offset" : 0,
"length" : 3,
"options" : [
{
"text" : "elasticsearch",
"_index" : "inputcompletion",
"_type" : "_doc",
"_id" : "FbaX5n0Bj82ZOUZeDDzH",
"_score" : 1.0,
"_source" : {
"input_completion" : "elasticsearch"
}
},
{
"text" : "elasticsearch api 中文",
"_index" : "inputcompletion",
"_type" : "_doc",
"_id" : "FLaX5n0Bj82ZOUZeDDzH",
"_score" : 1.0,
"_source" : {
"input_completion" : "elasticsearch api 中文"
}
},
{
"text" : "elasticsearch 教程",
"_index" : "inputcompletion",
"_type" : "_doc",
"_id" : "E7aX5n0Bj82ZOUZeDDzH",
"_score" : 1.0,
"_source" : {
"input_completion" : "elasticsearch 教程"
}
🍓四、Context Suggester带上下文的推荐
例如:再手机品类下搜索,小米提示小米手机,再食品下,提示真空小米
定义两种类型的 Context
Category – 任意的字符串
Geo – 地理位置信息
实现 Context Suggester 的具体步骤
1.定制⼀个 Mapping 索引数据
2.并且为每个⽂档加⼊ Context 信息
3.结合 Context 进⾏ Suggestion 查询
🌈4.1、例子
🌵4.1.1、定制⼀个 Mapping 索引数据
DELETE inputcompletion
PUT inputcompletion
{
"mappings": {
"properties": {
"input_completion": {
"type": "completion",
"contexts":[{
"type":"category",
"name":"goods_category"
}]
}
}
}
}
🌵4.1.2、并且为每个⽂档加⼊ Context 信息
POST inputcompletion/_doc
{
"comment":"小米手机",
"input_completion":{
"input":["小米"],
"contexts":{
"goods_category":"手机"
}
}
}
POST inputcompletion/_doc
{
"comment":"真空小米",
"input_completion":{
"input":["小米"],
"contexts":{
"goods_category":"食品"
}
}
}
🌵4.1.3、结合 Context 进⾏ Suggestion 查询
POST inputcompletion/_search
{
"suggest": {
"MY_SUGGESTION": {
"prefix": "小米",
"completion":{
"field":"input_completion",
"contexts":{
"goods_category":"食品"
}
}
}
}
}
🌵4.1.4、结果
{
"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" : {
"MY_SUGGESTION" : [
{
"text" : "小米",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "小米",
"_index" : "inputcompletion",
"_type" : "_doc",
"_id" : "F7aZ5n0Bj82ZOUZepzxT",
"_score" : 1.0,
"_source" : {
"comment" : "真空小米",
"input_completion" : {
"input" : [
"小米"
],
"contexts" : {
"goods_category" : "食品"
}
}
},
"contexts" : {
"goods_category" : [
"食品"
]
}
}
]
}
]
}
}