Elasticsearch Suggester智能搜索建议

1 Term Suggester
PUT /blogs/
{
“mappings”: {
“properties”: {
“body”:{
“type”: “text”
}
}
}
}

POST _bulk/?refresh=true
{ “index” : { “_index” : “blogs” } }
{ “body”: “Lucene is cool”}
{ “index” : { “_index” : “blogs” } }
{ “body”: “Elasticsearch builds on top of lucene”}
{ “index” : { “_index” : “blogs” } }
{ “body”: “Elasticsearch rocks”}
{ “index” : { “_index” : “blogs” } }
{ “body”: “Elastic is the company behind ELK stack”}
{ “index” : { “_index” : “blogs” } }
{ “body”: “elk rocks”}
{ “index” : { “_index” : “blogs”} }
{ “body”: “elasticsearch is rock solid”}

missing:考虑文本中出现的出现的相似词
POST /blogs/_search
{
“suggest”: {
“my-suggestion”: {
“text”: “lucne rock”,
“term”: {
“suggest_mode”: “missing”,
“field”: “body”
}
}
}
}
在这里插入图片描述

其中"rock"的options是空的,表示没有可以建议
的选项,为什么? 上面提到了,我们为查询提供的suggest mode是"missing",由于"rock"在索引的词典
里已经存在了,够精准,就不建议啦。 只有词典里找不到词,才会为其提供相似的选项
2 Phrase suggester
其在Term suggester的基础上,会考量多个term之间的关系,比如是否同时出现在索
引的原文里,相邻程度,以及词频等等
POST /blogs/_search
{
“suggest”: {
“my-suggestion”: {
“text”: “lucne and elasticsear rock”,
“phrase”: {
“field”: “body”,
“highlight”: {
“pre_tag”: “”,
“post_tag”: “

}
}
}
}
}
在这里插入图片描述
options直接返回一个phrase列表,由于加了highlight选项,被替换的term会被高亮。因为lucene和
elasticsearch曾经在同一条原文里出现过,同时替换2个term的可信度更高,所以打分较高,排在第一
位返回。Phrase suggester有相当多的参数用于控制匹配的模糊程度,需要根据实际应用情况去挑选和
调试

3 Completion Suggester
它主要针对的应用场景就是"Auto Completion"。 此场景下用户
每输入一个字符的时候,就需要即时发送一次查询请求到后端查找匹配项,在用户输入速度较高的情况
下对后端响应速度要求比较苛刻
PUT /blogs_completion/
{
“mappings”: {
“properties”: {
“body”: {
“type”: “completion”
}
}
}
}

POST _bulk/?refresh=true
{ “index” : { “_index” : “blogs_completion” } }
{ “body”: “Lucene is cool”}
{ “index” : { “_index” : “blogs_completion” } }
{ “body”: “Elasticsearch builds on top of lucene”}
{ “index” : { “_index” : “blogs_completion”} }
{ “body”: “Elasticsearch rocks”}
{ “index” : { “_index” : “blogs_completion” } }
{ “body”: “Elastic is the company behind ELK stack”}
{ “index” : { “_index” : “blogs_completion” } }
{ “body”: “the elk stack rocks”}
{ “index” : { “_index” : “blogs_completion”} }
{ “body”: “elasticsearch is rock solid”}

POST /blogs_completion/_search?pretty
{
“size”: 0,
“suggest”: {
“blog-suggest”: {
“prefix”: “elastic i”,
“completion”: {
“field”: “body”
}
}
}
}

在这里插入图片描述
总结:精准程度上(Precision)看: Completion > Phrase > term, 而召回率上(Recall)则反之。从性能上看,Completion Suggester是最快的,如果能满足业务需求,只用Completion Suggester做前缀匹配是最理想的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值