本文主要是介绍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做前缀匹配是最理想的
这篇关于Elasticsearch Suggester智能搜索建议的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!