ES安装中文分词器(版本6.2.2)
-
首先下载zip格式的压缩包
- https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip
- 将下载完成的zip文件放入es文件目录下的plugins文件夹
-
进入plugins文件夹
- cd /usr/soft/elasticsearch-6.2.2/plugins/
-
解压分词器文件
- unzip elasticsearch-analysis-ik-6.2.2.zip
-
PS:重要步骤
- 在解压完成后必须删除下载后的压缩包,否则重启es服务时会报错,无法正常加载
-
重启es服务
-
开启head与Postman进行测试
-
plugin [analysis-ik] loaded表示加载成功
-
测试用例:
-
创建索引与类型:
-
我这里索引名叫demo1,为方便测试仅添加一个属性
-
{ "settings": { "number_of_shards":3 }, "mappings":{ "userinfos":{ "properties":{ "username":{ "type":"text", "analyzer":"ik_max_word", "search_analyzer":"ik_max_word" } } } } } //关键语句:添加ik分词器 "analyzer":"ik_max_word", "search_analyzer":"ik_max_word"
-
添加测试数据
-
{ "username":"我爱你中国" } { "username":"中国真美丽" }
-
分词搜索语句:/userinfos/_search
-
{ "query":{ "match":{ "username":"中国" } }, "highlight":{ "pre_tags":[" ",""], "post_tags":[" ",""], "fields":{ "username":{ } } } } //这里在查询语句条件下使用代码高亮功能,搜索关键词为中国,用于区分搜索词和搜索结果,查看是否成功
-
结果:
-
{ "took": 6, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 0.18232156, "hits": [ { "_index": "demo1", "_type": "userinfos", "_id": "M-UzHGsBZm30MHGEFbNF", "_score": 0.18232156, "_source": { "username": "我爱你中国" }, "highlight": { "username": [ "我爱你 中国" ] } }, { "_index": "demo1", "_type": "userinfos", "_id": "NOUzHGsBZm30MHGEQrMp", "_score": 0.18232156, "_source": { "username": "中国真美丽" }, "highlight": { "username": [ "中国 真美丽" ] } } ] } } //说明一件根据'中国'关键词进行了分词搜索
-