之前我们创建索引,查询数据,都是使用的默认的分词器,分词效果不太理想,会把text的字段分成一个一个汉字,然后搜索的时候也会把搜索的句子进行分词,所以这里就需要更加智能的分词器IK分词器了。
1. ik分词器的下载和安装,测试
第一: 下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases ,这里你需要根据你的Es的版本来下载对应版本的IK,这里我使用的是6.3.2的ES,所以就下载ik-6.3.2.zip的文件。

第二: 解压-->将文件复制到 es的安装目录/plugin/ik下面即可,完成之后效果如下:

到这里已经完成了,不需要去elasticSearch的 elasticsearch.yml 文件去配置。
第三:重启ElasticSearch
第四:测试效果
未使用ik分词器的时候测试分词效果:
1POST book/_analyze 2{ 3 "text": "我是中国人" 4} 5//结果是: 6{ 7 "tokens": [ 8 { 9 "token": "我", 10 "start_offset": 0, 11 "end_offset": 1, 12 "type": "<IDEOGRAPHIC>", 13 "position": 0 14 }, 15 { 16 "token": "是", 17 "start_offset": 1, 18 "end_offset": 2, 19 "type": "<IDEOGRAPHIC>", 20 "position": 1 21 }, 22 { 23 "token": "中", 24 "start_offset": 2, 25 "end_offset": 3, 26 "type": "<IDEOGRAPHIC>", 27 "position": 2 28 }, 29 { 30 "token": "国", 31 "start_offset": 3, 32 "end_offset": 4, 33 "type": "<IDEOGRAPHIC>", 34 "position": 3 35 }, 36 { 37 "token": "人", 38 "start_offset": 4, 39 "end_offset": 5, 40 "type": "<IDEOGRAPHIC>", 41 "position": 4 42 } 43 ] 44}
使用IK分词器之后,结果如下:
1POST book_v6/_analyze 2{ 3 "analyzer": "ik_max_word", 4 "text": "我是中国人" 5} 6//结果如下: 7{ 8 "tokens": [ 9 { 10 "token": "我", 11 "start_offset": 0, 12 "end_offset": 1, 13 "type": "CN_CHAR", 14 "position": 0 15 }, 16 { 17 "token": "是", 18 "start_offset": 1, 19 "end_offset": 2, 20 "type": "CN_CHAR", 21 "position": 1 22 }, 23 { 24 "token": "中国人", 25 "start_offset": 2, 26 "end_offset": 5, 27 "type": "CN_WORD", 28 "position": 2 29 }, 30 { 31 "token": "中国", 32 "start_offset": 2, 33 "end_offset": 4, 34 "type": "CN_WORD", 35 "position": 3 36 }, 37 { 38 "token": "国人", 39 "start_offset": 3, 40 "end_offset": 5, 41 "type": "CN_WORD", 42 "position": 4 43 } 44 ] 45}
对于上面两个分词效果的解释:
1. 如果未安装ik分词器,那么,你如果写 "analyzer": "ik_max_word",那么程序就会报错,因为你没有安装ik分词器
2. 如果你安装了ik分词器之后,你不指定分词器,不加上 "analyzer": "ik_max_word" 这句话,那么其分词效果跟你没有安装ik分词器是一致的,也是分词成每个汉字。
2. 创建指定分词器的索引
索引创建之后就可以使用ik进行分词了,当你使用ES搜索的时候也会使用ik对搜索语句进行分词,进行匹配。
1PUT book_v5 2{ 3 "settings":{ 4 "number_of_shards": "6", 5 "number_of_replicas": "1", 6 //指定分词器 7 "analysis":{ 8 "analyzer":{ 9 "ik":{ 10 "tokenizer":"ik_max_word" 11 } 12 } 13 } 14 }, 15 "mappings":{ 16 "novel":{ 17 "properties":{ 18 "author":{ 19 "type":"text" 20 }, 21 "wordCount":{ 22 "type":"integer" 23 }, 24 "publishDate":{ 25 "type":"date", 26 "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd" 27 }, 28 "briefIntroduction":{ 29 "type":"text" 30 }, 31 "bookName":{ 32 "type":"text" 33 } 34 } 35 } 36 } 37}
关于ik分词器的分词类型(可以根据需求进行选择):
ik_max_word:会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart:会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。如下:
1POST book_v6/_analyze 2{ 3 "analyzer": "ik_smart", 4 "text": "我是中国人" 5} 6//结果 7{ 8 "tokens": [ 9 { 10 "token": "我", 11 "start_offset": 0, 12 "end_offset": 1, 13 "type": "CN_CHAR", 14 "position": 0 15 }, 16 { 17 "token": "是", 18 "start_offset": 1, 19 "end_offset": 2, 20 "type": "CN_CHAR", 21 "position": 1 22 }, 23 { 24 "token": "中国人", 25 "start_offset": 2, 26 "end_offset": 5, 27 "type": "CN_WORD", 28 "position": 2 29 } 30 ] 31}