文章目录
- 1. 自定义分词器
- 2. 映射模型
- 3. 效果图
1. 自定义分词器
ES如何支持拼音和中文分词 ?
自定义分词器 支持拼音和中文分词
PUT /jd_goods
{
"settings": {
"analysis": {
"analyzer": {
"ik_smart_pinyin": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["my_pinyin", "word_delimiter"]
},
"ik_max_word_pinyin": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["my_pinyin", "word_delimiter"]
}
},
"filter": {
"my_pinyin": {
"type" : "pinyin",
"keep_separate_first_letter" : true,
"keep_full_pinyin" : true,
"keep_original" : true,
"limit_first_letter_length" : 16,
"lowercase" : true,
"remove_duplicated_term" : true
}
}
}
}
}
2. 映射模型
重新指定文档类型映射拼音分词类型
POST /jd_goods/_mapping
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word_pinyin",
"search_analyzer": "ik_smart_pinyin"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word_pinyin",
"search_analyzer": "ik_smart_pinyin"
},
"img": {
"type": "text"
},
"price": {
"type": "text"
}
}
}
1.和2.可以合并为下面这一条命令
PUT /jd_goods
{
"settings": {
"analysis": {
"analyzer": {
"ik_smart_pinyin": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": ["my_pinyin", "word_delimiter"]
},
"ik_max_word_pinyin": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": ["my_pinyin", "word_delimiter"]
}
},
"filter": {
"my_pinyin": {
"type" : "pinyin",
"keep_separate_first_letter" : true,
"keep_full_pinyin" : true,
"keep_original" : true,
"limit_first_letter_length" : 16,
"lowercase" : true,
"remove_duplicated_term" : true
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word_pinyin",
"search_analyzer": "ik_smart_pinyin"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word_pinyin",
"search_analyzer": "ik_smart_pinyin"
},
"img": {
"type": "text"
},
"price": {
"type": "text"
}
}
}
}
3. 效果图
- 拼音搜索
- 中文搜索
具体详细:
请参考elasticsearch-7.15.2 配置IK中文分词器+拼音分词