- 建索引
PUT business_line
{
"mappings": {
"business" : {
"properties" : {
"name" : {
"type" : "completion",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc" : {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}
- 添加数据
POST business_line/business?refresh
{
"name" : "京东人寿险",
"desc": "京东人寿险是个十分棒的业务"
}
- 测试补全功能
POST business_line/_search?pretty
{
"suggest": {
"song-suggest" : {
"prefix" : "京东",
"completion" : {
"field" : "name"
}
}
}
}
测试发现补全功能只支持前缀补全,原理是指定type为completion类型之后,会将分词结果在内存中保存一份,保存的数据结构为tries树。同时,type类型不为completion类型的字段不支持补全查询。以上结论可以自行验证。
如果想要支持对所有输入的词汇进行联想查询,可以参照这篇文章:https://www.elastic.co/cn/blog/you-complete-me
4. 补全功能扩展:
PUT novel
{
"mappings": {
"novel" : {
"properties" : {
"all_input" : {
"type" : "completion",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"name" : {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"author" : {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc" : {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}
input选项为分词结果
POST _analyze
{
"analyzer": "ik_max_word",
"text": "四大名著"
}
POST novel/novel?refresh
{
"all_input" : {
"input": [
"三国演义",
"三国",
"演义",
"罗贯中",
"四大",
"名著",
"四大名著"
]
},
"output":"三国演义",
"name": "三国演义",
"author": "罗贯中",
"desc": "四大名著"
}
POST novel/_search?pretty
{
"suggest": {
"song-suggest" : {
"text" : "三国",
"completion" : {
"field" : "all_input"
}
}
},
"_source": "output"
}
但是这个功能个人感觉有点鸡肋,词汇的录入需要人工干预,不够智能化;而且在5.0以后,output不再支持在字段级别进行配置,需要放到_source中,所以整个补全过程需要查询出文档,拿出文档的output字段。