1. 什么是搜索建议?
帮助⽤户在搜索的过程,对输入中进行纠错,提示建议性词语。
例如:
2. ES实现原理
输⼊的⽂本分解为 Token,然后在索引的字典里查找相似的 Term 并返回。
3. 对应API,Term& Phrase Suggester
TermSuggester
3种Suggestion Mode
Missing – 如索引中已经存在,就不提供建议
Popular – 推荐出现频率更加⾼的词
Always – ⽆论是否存在,都提供建议
一个排序
默认按照 score 排序,也可以按照“frequency”
首字母限制
默认⾸字⺟不⼀致,就不会匹配推荐。解决方法,将 prefix_length 设置为 0,
Phrase Suggester
Suggest Mode :missing, popular, always
Max Errors:最多可以拼错的 Terms 数
Confidence:限制返回结果数,默认为1,只有分数高过设定值时,才会返回。
4. 例子
代码语言:javascript复制# 删除之前的Index
DELETE articles
# 插入2个测试数据
POST articles/_bulk
{ "index" : { } }
{ "body": "elasticsearch is good"}
{ "index" : { } }
{ "body": "elasticsearch is ok"}
2. 调用查询api
代码语言:javascript复制POST /articles/_search
{
"size": 1,
"query": {
"match": {
"body": "elasticseach"
}
},
"suggest": {
"term-suggestion": {
"text": "elasticseach",
"term": {
"suggest_mode": "missing",
"field": "body"
}
}
}
}
对应结果
代码语言:javascript复制{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"suggest" : {
"term-suggestion" : [
{
"text" : "elasticseach",
"offset" : 0,
"length" : 12,
"options" : [
{
"text" : "elasticsearch",
"score" : 0.9166667,
"freq" : 2
}
]
}
]
}
}
option部分就是对应的推荐词
5. 引用文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-suggesters.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-suggesters-term.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.1/search-suggesters-phrase.html