在输入框输入关键词的时候,有自动补全功能(此处需要定义一个选择器),选择要输入的关键词,点击查询(点击查询以后,需要使用到另一个分词器对关键词进行分词),显示结果
注:
输入关键词的时候,使用分词器1
查询的时候,使用分词器2
PUT news
{
"settings":{
"analysis":{
"analyzer":{
#自定义分词器名称
"tag_analyzer":{
"type":"custom",
"char_filter":["html_strip"],
"tokenizer":"keyword",
"filter":["pinyin_filter","completed_filter"]
},
#输入查询关键词时使用的分词器,
"tag_search_analyzer":{
"type":"custom",
"char_filter":["html_strip"],
"tokenizer":"keyword",
"filter":["lowercase"]
}
},
"filter":{
"completed_filter":{
"type":"edge_ngram",
"min_gram":1,
"max_gram":15
},
"pinyin_filter":{
"type":"pinyin",
"keep_first_letter":true,
"keep_full_pinyin":false,
"keep_joined_full_pinyin":true,
"keep_none_chinese":true,
"keep_none_chinese_together":true,
"keep_none_chinese_in_first_letter":true,
"keep_none_chinese_in_joined_full_pinyin":true,
"none_chinese_pinyin_tokenize":false,
"keep_original":true,
"remove_duplicated_term":true
}
}
}
}
}
GET news/_analyze
{
"analyzer":"tag_search_analyzer",
"text":"JAVA"
}
GET news/_analyze
{
"analyzer":"tag_analyzer",
"text":"刘德华"
}
tag_analyzer分词器结果如下:
刘
刘德
刘德华
l
li
liu
liud
liude
liudeh
liudehu
liudehua
l
ld
ldh
注:
PUT news/_mapping
{
"properties":{
"tags":{
#使用前缀推荐,所以type为completion
"type":"completion",
"analyzer":"tag_analyzer",
#指定查询时使用到的分词器
"search_analyzer":"tag_search_analyzer"
}
}
}
POST news/_bulk
{"index":{}}
{"tags":["刘德华","华语乐坛","黎明"]}
GET news/_search
{
"suggest":{
"tag-suggest":{
"prefix":"lim",
"completion":{
"field":"tags"
}
}
}
}