搜索推荐:类似百度,下拉框自动匹配输入的内容
最后一个term前缀搜索,前面的 match搜索,结合前两搜索筛选出结果
max_expansions:指定prefix最多匹配多少个term,超过就不匹配了,限定性能(大数据)
ngram和index-time搜索推荐原理
1、什么是ngram
quick,5种长度下的ngram:
ngram length=1, q u i c k
ngram length=2, qu ui ic ck
ngram length=3, qui uic ick
ngram length=4, quic uick
ngram length=5, quick
2、什么是edge ngram
quick,anchor首字母后进行ngram
q
qu
qui
quic
quick 看到区别了吗?
使用edge ngram将每个单词都进行进一步的分词切分,用切分后的ngram来实现前缀搜索推荐功能 :
hello world
hello we
h
he
hel
hell
hello
w
wo
wor
worl
world
搜索时不需要根据前缀扫描整个索引,直接匹配,match操作,全文检索所以包含hello没有w的也会被搜索出来,可以用match_phrase卡一下
创建索引时可以直接指定长度,发现在哪里指定了吗?
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 3
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
超过长度(3)之后的单词的就不要了:hello——> hel
添加分词器