示例 - 黑马旅游
# 自动补全
# 修改酒店映射结构
// 酒店数据索引库
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
// 自定义分词器1:分词后转拼音(用于全文检索)
"text_anlyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
},
// 自定义分词器2:不分词转拼音(用于自动补全)
"completion_analyzer": {
"tokenizer": "keyword",
"filter": "py"
}
},
"filter": {
// 分词过滤器:py
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart"
},
// 用于自动补全
"suggestion":{
"type": "completion",
"analyzer": "completion_analyzer"
}
}
}
}
# 测试自动补全
GET /hotel/_search
{
"suggest": {
// 自定义补全名称
"suggestions": {
// 输入内容
"text": "h",
"completion": {
"field": "suggestion",
// 跳过重复
"skip_duplicates": true,
"size": 10
}
}
}
}
上次更新: 2023/05/20, 16:38:38