elasticsearch ik 中文分词 安装配置

张映 发表于 2018-04-26

分类目录: elasticsearch, 服务器相关

标签:, , ,

elasticsearch自带有中文分词,但是特别的傻,后面会做对比,在这里推荐analysis ik,用es来做全文检索工具的人员80%-90%会用这个中文分词工具,一直在更新维护。

1,elasticsearch分词器(analyzers)说明

elasticsearch中,内置了很多分词器(analyzers),例如standard (标准分词器)、english (英文分词)和chinese (中文分词)。

其中standard 就是无脑的一个一个词(汉字)切分,所以适用范围广,但是精准度低;

english 对英文更加智能,可以识别单数负数,大小写,过滤stopwords(例如“the”这个词)等;

2,安装maven

  1. $ brew search maven      //mac  
  2. # apt-get install maven  //ubuntu  
  3. # yum install maven      //centos or redhat  
  4.   
  5. $ mvn -v  
  6. Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)  
  7. Maven home: /usr/local/Cellar/maven/3.5.0/libexec  
  8. Java version: 1.8.0_112, vendor: Oracle Corporation  
  9. Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre  
  10. Default locale: zh_CN, platform encoding: UTF-8  
  11. OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"  

3,下载analysis ik插件

  1. $ git clone https://github.com/medcl/elasticsearch-analysis-ik.git  
  2. $ cd elasticsearch-analysis-ik  
  3. $ git branch -a    //根据不同的es版本,进行git checkout  
  4. * master //主分支是6.2.3的  
  5.  remotes/origin/2.x  
  6.  remotes/origin/5.3.x  
  7.  remotes/origin/5.x  
  8.  remotes/origin/6.1.x  
  9.  remotes/origin/HEAD -> origin/master  
  10.  remotes/origin/arkxu-master  
  11.  remotes/origin/master  
  12.  remotes/origin/revert-80-patch-1  
  13.  remotes/origin/rm  
  14.  remotes/origin/wyhw-ik_lucene4  
  15.   
  16. $ mvn package  //打包  
  17.   
  18. $ ll target/releases/  
  19. total 4400  
  20. drwxr-xr-x 3 zhangying staff 102 4 24 13:46 ./  
  21. drwxr-xr-x 11 zhangying staff 374 4 24 13:32 ../  
  22. -rw-r--r-- 1 zhangying staff 4501993 4 24 13:32 elasticsearch-analysis-ik-6.2.3.zip  
  23.   
  24. //在releases目录会生成一个zip文件,将其解压  
  25. $ cd target/releases/ && unzip elasticsearch-analysis-ik-6.2.3.zip  

4,安装analysis ik插件

  1. $ brew info elasticsearch  
  2. elasticsearch: stable 6.2.3, HEAD  
  3. Distributed search & analytics engine  
  4.   
  5. https://www.elastic.co/products/elasticsearch  
  6.   
  7. /usr/local/Cellar/elasticsearch/6.2.3 (112 files, 30.8MB) *  
  8.  Built from source on 2018-04-24 at 14:17:01  
  9. From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/elasticsearch.rb  
  10. ==> Requirements  
  11. Required: java = 1.8 ✔  
  12. ==> Options  
  13. --HEAD  
  14.  Install HEAD version  
  15. ==> Caveats  
  16. Data: /usr/local/var/lib/elasticsearch/elasticsearch_zhangying/  
  17. Logs: /usr/local/var/log/elasticsearch/elasticsearch_zhangying.log  
  18. Plugins: /usr/local/var/elasticsearch/plugins/  //插件路径  
  19. Config: /usr/local/etc/elasticsearch/  
  20.   
  21. To have launchd start elasticsearch now and restart at login:  
  22.  brew services start elasticsearch  
  23. Or, if you don't want/need a background service you can just run:  
  24.  elasticsearch  
  25.   
  26. //将刚才解压出来目录,移动plugins下面  
  27. $ mv elasticsearch /usr/local/var/elasticsearch/plugins/ik  

在这里要注意,不要在elasticsearch.yml文件中加index:analysis:analyzer:,老版支持,但是es6.x尝试了几种办法都没有成功,会报以下错误:

node settings must not contain any index level settings

5,启动elasticsearch

  1. $ elasticsearch  //启动  

如果出现以下内容就说成功了

analysis-ik 中文分词

analysis-ik 中文分词

6,测试中文分词

  1. //创建索引  
  2. $ curl -XPUT "http://127.0.0.1:9200/tank?pretty"   
  3.   
  4. //创建mapping  
  5. $ curl -XPOST "http://127.0.0.1:9200/tank/chinese/_mapping?pretty" -H "Content-Type: application/json" -d ' 
  6. { 
  7.     "chinese": { 
  8.             "_all":{ 
  9.               "enabled":false //禁止全字段全文检索 
  10.             }, 
  11.             "properties": { 
  12.                 "id": { 
  13.                     "type": "integer" 
  14.                 }, 
  15.                 "username": { 
  16.                     "type": "text", 
  17.                     "analyzer": "ik_max_word" //精确分词模式 
  18.                 }, 
  19.                 "description": { 
  20.                     "type": "text", 
  21.                     "analyzer": "ik_max_word" 
  22.                 } 
  23.             } 
  24.         } 
  25.   } 
  26. '  
  27. //插入二条数据  
  28. $ curl -XPOST "http://127.0.0.1:9200/tank/chinese/?pretty"  -H "Content-Type: application/json" -d ' 
  29. { 
  30.     "id" : 1, 
  31.     "username" :  "中国高铁速度很快", 
  32.     "description" :  "如果要修改一个字段的类型" 
  33. }'  
  34.   
  35. $ curl -XPOST "http://127.0.0.1:9200/tank/chinese/?pretty"  -H "Content-Type: application/json" -d ' 
  36. { 
  37.     "id" : 2, 
  38.     "username" :  "动车和复兴号,都属于高铁", 
  39.     "description" :  "现在想要修改为string类型" 
  40. }'  
  41.   
  42. //搜索  
  43. $ curl -XPOST "http://127.0.0.1:9200/tank/chinese/_search?pretty"  -H "Content-Type: application/json"  -d ' 
  44. > { 
  45. >     "query": { 
  46. >         "match": { 
  47. >             "username": "中国高铁" 
  48. >         } 
  49. >     } 
  50. > } 
  51. > '  
  52. {  
  53.   "took" : 188,  
  54.   "timed_out" : false,  
  55.   "_shards" : {  
  56.     "total" : 5,  
  57.     "successful" : 5,  
  58.     "skipped" : 0,  
  59.     "failed" : 0  
  60.   },  
  61.   "hits" : {  
  62.     "total" : 2,  
  63.     "max_score" : 0.8630463,  
  64.     "hits" : [  
  65.       {  
  66.         "_index" : "tank",  
  67.         "_type" : "chinese",  
  68.         "_id" : "oJfx_2IBVvjz0l6TkJ6K",  
  69.         "_score" : 0.8630463,  //权重越高,匹配度越大  
  70.         "_source" : {  
  71.           "id" : 1,  
  72.           "username" : "中国高铁速度很快",  
  73.           "description" : "如果要修改一个字段的类型"  
  74.         }  
  75.       },  
  76.       {  
  77.         "_index" : "tank",  
  78.         "_type" : "chinese",  
  79.         "_id" : "oZfx_2IBVvjz0l6Tpp64",  
  80.         "_score" : 0.5753642,  
  81.         "_source" : {  
  82.           "id" : 2,  
  83.           "username" : "动车和复兴号,都属于高铁",  
  84.           "description" : "现在想要修改为string类型"  
  85.         }  
  86.       }  
  87.     ]  
  88.   }  
  89. }  

7,elasticsearch内置中文分词和ik分词对比

  1. $ curl -XPOST 'http://localhost:9200/tank/_analyze?pretty=true' -H 'Content-Type: application/json' -d ' 
  2. > { 
  3. > "analyzer":"ik_smart",  //简短分词 
  4. > "text":"感叹号" 
  5. > }'  
  6. {  
  7.  "tokens" : [  
  8.  {  
  9.  "token" : "感叹号",  
  10.  "start_offset" : 0,  
  11.  "end_offset" : 3,  
  12.  "type" : "CN_WORD",  
  13.  "position" : 0  
  14.  }  
  15.  ]  
  16. }  
  17.   
  18. $ curl -XPOST 'http://localhost:9200/tank/_analyze?pretty=true' -H 'Content-Type: application/json' -d ' 
  19. > { 
  20. > "analyzer":"standard",  //es自带分词 
  21. > "text":"感叹号" 
  22. > }'  
  23. {  
  24.  "tokens" : [  
  25.  {  
  26.  "token" : "感",  
  27.  "start_offset" : 0,  
  28.  "end_offset" : 1,  
  29.  "type" : "<IDEOGRAPHIC>",  
  30.  "position" : 0  
  31.  },  
  32.  {  
  33.  "token" : "叹",  
  34.  "start_offset" : 1,  
  35.  "end_offset" : 2,  
  36.  "type" : "<IDEOGRAPHIC>",  
  37.  "position" : 1  
  38.  },  
  39.  {  
  40.  "token" : "号",  
  41.  "start_offset" : 2,  
  42.  "end_offset" : 3,  
  43.  "type" : "<IDEOGRAPHIC>",  
  44.  "position" : 2  
  45.  }  
  46.  ]  
  47. }  
  48.   
  49. $ curl -XPOST 'http://localhost:9200/tank/_analyze?pretty=true' -H 'Content-Type: application/json' -d ' 
  50. > { 
  51. > "analyzer":"ik_max_word",  //精确分词 
  52. > "text":"感叹号" 
  53. > }'  
  54. {  
  55.  "tokens" : [  
  56.  {  
  57.  "token" : "感叹号",  
  58.  "start_offset" : 0,  
  59.  "end_offset" : 3,  
  60.  "type" : "CN_WORD",  
  61.  "position" : 0  
  62.  },  
  63.  {  
  64.  "token" : "感叹",  
  65.  "start_offset" : 0,  
  66.  "end_offset" : 2,  
  67.  "type" : "CN_WORD",  
  68.  "position" : 1  
  69.  },  
  70.  {  
  71.  "token" : "叹号",  
  72.  "start_offset" : 1,  
  73.  "end_offset" : 3,  
  74.  "type" : "CN_WORD",  
  75.  "position" : 2  
  76.  }  
  77.  ]  
  78. }  


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1892.html