文章目录
- 1. 下载分词器
- 2. es集成pinyin
- 3. 启动es
- 4. 自定义分词
- 5. 映射模型
- 6. 初始化数据
- 7. 查询索引
- 8. 效果图
- 9. 开源项目
1. 下载分词器
- ik中文分词器
中文分词器:https://github.com/medcl/elasticsearch-analysis-ik
- 拼音分词器
链接:https://github.com/medcl/elasticsearch-analysis-pinyin
2. es集成pinyin
方式任选其中一种即可
- 第一种:在线安装
cd elasticsearch-7.15.2
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.15.2/elasticsearch-analysis-ik-7.15.2.zip
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.15.2/elasticsearch-analysis-pinyin-7.15.2.zip
- 第二种:下载解压
- pinyin分词
解压elasticsearch-analysis-pinyin-7.15.2.zip
,把解压后的文件夹elasticsearch-analysis-pinyin-7.15.2
复制到elasticsearch-7.15.2\plugins
目录下面,并重命名为analysis-pinyin
unzip elasticsearch-analysis-pinyin-7.15.2.zip
mv elasticsearch-analysis-pinyin-7.15.2 elasticsearch-7.15.2/plugins analysis-pinyin
- ik中文分词
解压elasticsearch-analysis-ik-7.15.2.zip
,把解压后的文件夹elasticsearch-analysis-ik-7.15.2
复制到elasticsearch-7.15.2\plugins
目录下面,并重命名为analysis-ik
unzip elasticsearch-analysis-ik-7.15.2.zip
mv elasticsearch-analysis-ik-7.15.2 elasticsearch-7.15.2/plugins analysis-ik
3. 启动es
- windows
- linux
cd elasticsearch-7.15.2
bin/elasticsearch
4. 自定义分词
自定义分词:同时支持ik中文分词和pinyin分词
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}}}}
}
5. 映射模型
重新指定文档类型映射拼音分词类型
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"}}
}
6. 初始化数据
PUT /gblfy/user/1
{"title":"女士包","img":"http://xxx.png","price":"22","desc": "一顿操作猛如虎,一看工资2500"
}
7. 查询索引
- pinyi查询
GET jd_goods/_search
{"query": {"match": {"title": "nvbao"}}
}
- ik中文查询
GET jd_goods/_search
{"query": {"match": {"title": "女包"}}
}
8. 效果图
9. 开源项目
https://gitee.com/gblfy/es7-jd-vue