Elasticsearch安装中文分词插件ik

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介:

Elasticsearch默认提供的分词器,会把每个汉字分开,而不是我们想要的根据关键词来分词。例如:

[html] view plain copy

  1. curl -XPOST  "http://localhost:9200/userinfo/_analyze?analyzer=standard&pretty=true&text=我是中国人"  

我们会得到这样的结果:


[html] view plain copy

  1. {  

  2. tokens: [  

  3. {  

  4. token: text  

  5. start_offset: 2  

  6. end_offset: 6  

  7. type: <ALPHANUM>  

  8. position: 1  

  9. }  

  10. {  

  11. token: 我  

  12. start_offset: 9  

  13. end_offset: 10  

  14. type: <IDEOGRAPHIC>  

  15. position: 2  

  16. }  

  17. {  

  18. token: 是  

  19. start_offset: 10  

  20. end_offset: 11  

  21. type: <IDEOGRAPHIC>  

  22. position: 3  

  23. }  

  24. {  

  25. token: 中  

  26. start_offset: 11  

  27. end_offset: 12  

  28. type: <IDEOGRAPHIC>  

  29. position: 4  

  30. }  

  31. {  

  32. token: 国  

  33. start_offset: 12  

  34. end_offset: 13  

  35. type: <IDEOGRAPHIC>  

  36. position: 5  

  37. }  

  38. {  

  39. token: 人  

  40. start_offset: 13  

  41. end_offset: 14  

  42. type: <IDEOGRAPHIC>  

  43. position: 6  

  44. }  

  45. ]  

  46. }  

正常情况下,这不是我们想要的结果,比如我们更希望 “中国人”,“中国”,“我”这样的分词,这样我们就需要安装中文分词插件,ik就是实现这个功能的。


elasticsearch-analysis-ik 是一款中文的分词插件,支持自定义词库。

安装步骤:

1、到github网站下载源代码,网站地址为:https://github.com/medcl/elasticsearch-analysis-ik

右侧下方有一个按钮“Download ZIP",点击下载源代码elasticsearch-analysis-ik-master.zip。

2、解压文件elasticsearch-analysis-ik-master.zip,进入下载目录,执行命令:

[html] view plain copy

  1. unzip elasticsearch-analysis-ik-master.zip  


3、将解压目录文件中config/ik文件夹复制到ES安装目录config文件夹下。

4、因为是源代码,此处需要使用maven打包,进入解压文件夹中,执行命令:

[html] view plain copy

  1. mvn clean package  

5、将打包得到的jar文件elasticsearch-analysis-ik-1.2.8-sources.jar复制到ES安装目录的lib目录下。

6、在ES的配置文件config/elasticsearch.yml中增加ik的配置,在最后增加:

[html] view plain copy

  1. index:  

  2.   analysis:                     

  3.     analyzer:        

  4.       ik:  

  5.           alias: [ik_analyzer]  

  6.           type: org.elasticsearch.index.analysis.IkAnalyzerProvider  

  7.       ik_max_word:  

  8.           type: ik  

  9.           use_smart: false  

  10.       ik_smart:  

  11.           type: ik  

  12.           use_smart: true  

[html] view plain copy

  1. index.analysis.analyzer.ik.type : “ik”  

7、重新启动elasticsearch服务,这样就完成配置了,收入命令:

[html] view plain copy

  1. curl -XPOST  "http://localhost:9200/userinfo/_analyze?analyzer=ik&pretty=true&text=我是中国人"  

测试结果如下:

[html] view plain copy

  1. {  

  2. tokens: [  

  3. {  

  4. token: text  

  5. start_offset: 2  

  6. end_offset: 6  

  7. type: ENGLISH  

  8. position: 1  

  9. }  

  10. {  

  11. token: 我  

  12. start_offset: 9  

  13. end_offset: 10  

  14. type: CN_CHAR  

  15. position: 2  

  16. }  

  17. {  

  18. token: 中国人  

  19. start_offset: 11  

  20. end_offset: 14  

  21. type: CN_WORD  

  22. position: 3  

  23. }  

  24. {  

  25. token: 中国  

  26. start_offset: 11  

  27. end_offset: 13  

  28. type: CN_WORD  

  29. position: 4  

  30. }  

  31. {  

  32. token: 国人  

  33. start_offset: 12  

  34. end_offset: 14  

  35. type: CN_WORD  

  36. position: 5  

  37. }  

  38. ]  

  39. }  

说明:


1、ES安装插件本来使用使用命令plugin来完成,但是我本机安装ik时一直不成功,所以就使用源代码打包安装了。

2、自定义词库的方式,请参考 https://github.com/medcl/elasticsearch-analysis-ik

本文转自  陈小龙哈   51CTO博客,原文链接:http://blog.51cto.com/chenxiaolong/1895121


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
19天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
108 0
|
6天前
|
JSON Unix Linux
Elasticsearch如何安装
Elasticsearch如何安装
|
1月前
|
监控 安全 Java
ElasticSearch在Windows上的下载与安装
ElasticSearch在Windows上的下载与安装
|
2月前
|
开发工具 Docker 容器
docker安装集群版ElasticSearch
docker安装集群版ElasticSearch
|
2月前
|
Java Docker 容器
Docker安装ElasticSearch
Docker如何安装ElasticSearch
|
2月前
|
自然语言处理
Elasticsearch+IK+pinyin自定义分词器
Elasticsearch+IK+pinyin自定义分词器
27 0
|
2月前
|
存储 监控 搜索推荐
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
|
4月前
ElasticSearch-Head浏览器插件离线安装
ElasticSearch-Head浏览器插件离线安装
90 0
|
3月前
|
前端开发 安全 Ubuntu
Elasticsearch安装和配置
Elasticsearch安装和配置
116 0