Elasticsearch安装中文分词插件ik

http://blog.csdn.net/liuzhenfeng/article/details/39404435

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

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

我们会得到这样的结果:

  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. }  
{
tokens: [
{
token: text
start_offset: 2
end_offset: 6
type: <ALPHANUM>
position: 1
}
{
token: 我
start_offset: 9
end_offset: 10
type: <IDEOGRAPHIC>
position: 2
}
{
token: 是
start_offset: 10
end_offset: 11
type: <IDEOGRAPHIC>
position: 3
}
{
token: 中
start_offset: 11
end_offset: 12
type: <IDEOGRAPHIC>
position: 4
}
{
token: 国
start_offset: 12
end_offset: 13
type: <IDEOGRAPHIC>
position: 5
}
{
token: 人
start_offset: 13
end_offset: 14
type: <IDEOGRAPHIC>
position: 6
}
]
}

正常情况下,这不是我们想要的结果,比如我们更希望 “中国人”,“中国”,“我”这样的分词,这样我们就需要安装中文分词插件,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,进入下载目录,执行命令:

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

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

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

  1. mvn clean package  
mvn clean package

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

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

  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  
index:
  analysis:                   
    analyzer:      
      ik:
          alias: [ik_analyzer]
          type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
          type: ik
          use_smart: false
      ik_smart:
          type: ik
          use_smart: true

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

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

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

测试结果如下:

  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. }  
{
tokens: [
{
token: text
start_offset: 2
end_offset: 6
type: ENGLISH
position: 1
}
{
token: 我
start_offset: 9
end_offset: 10
type: CN_CHAR
position: 2
}
{
token: 中国人
start_offset: 11
end_offset: 14
type: CN_WORD
position: 3
}
{
token: 中国
start_offset: 11
end_offset: 13
type: CN_WORD
position: 4
}
{
token: 国人
start_offset: 12
end_offset: 14
type: CN_WORD
position: 5
}
]
}

说明:

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

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

 

附:

2.安装IK插件:(1.2.9没有jar包,要自己打)


要与ES版本配套:

Version
-—————
master | 1.4.0 → master
1.2.9 | 1.4.0
1.2.8 | 1.3.2
1.2.7 | 1.2.1


下载elasticsearch-analysis-ik 源代码 https://github.com/medcl/elasticsearch-analysis-ik

cd  elasticsearch-analysis-ik
 
mvn package
 
cd  target /releases/


就会看到    elasticsearch-analysis-ik-1.2.9.zip 

把elasticsearch-analysis-ik-1.2.9.zip 解压到 ES/plugins/analysis-ik/ 

root@blog-mreald-com: /usr/share/elasticsearch/plugins/analysis-ik # ls
commons-codec-1.6.jar      elasticsearch-analysis-ik-1.2.9.jar  httpclient-4.3.5.jar
commons-logging-1.1.3.jar  elasticsearch-analysis-ik-1.2.9.zip  httpcore-4.3.2.jar

将ik的配置和字典都复制到ES_HOME/config下

sudo  cp  -R ik  /etc/elasticsearch

elasticsearch.yml 增加下面一行,然后重启下ES

index.analysis.analyzer.ik.type : 'ik'


3.IK 安装之后:

root@blog-mreald-com: /usr/share/elasticsearch # curl -XGET 'http://localhost:9200/posts/_analyze?analyzer=ik&pretty=true&text=Mreald致力于全栈工程师'


查看结果:

{
   "tokens"  : [ {
     "token"  "mreald" ,
     "start_offset"  : 1,
     "end_offset"  : 7,
     "type"  "ENGLISH" ,
     "position"  : 1
   }, {
     "token"  "致力于" ,
     "start_offset"  : 8,
     "end_offset"  : 11,
     "type"  "CN_WORD" ,
     "position"  : 2
   }, {
     "token"  "致力" ,
     "start_offset"  : 8,
     "end_offset"  : 10,
     "type"  "CN_WORD" ,
     "position"  : 3
   }, {
     "token"  "致" ,
     "start_offset"  : 8,
     "end_offset"  : 9,
     "type"  "CN_WORD" ,
     "position"  : 4
   }, {
   ...................

2.错误:

{
  "error" : "NoShardAvailableActionException[[_na][_na] No shard available for
  [org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest@7c9c15]]",
  "status" : 503
}

cp 过去的是: target/releases/elasticsearch-analysis-ik-1.2.9.zip 

    不是: target/elasticsearch-analysis-ik-1.2.9.jar

http://blog.mreald.com/160

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Elasticsearch安装中文分词IK,请按照以下步骤操作: 1.确保您的Elasticsearch版本与IK分词器版本兼容。您可以在IK分词器的GitHub页面上查看兼容性信息。 2.下载IK分词器插件。您可以在IK分词器的GitHub页面上找到最新版本的插件。 3.将IK分词器插件安装Elasticsearch中。您可以使用以下命令将插件安装Elasticsearch中: ``` sudo bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v{版本号}/elasticsearch-analysis-ik-{版本号}.zip ``` 请将{版本号}替换为您要安装IK分词器的版本号。例如,如果您要安装版本7.5.1的IK分词器,则应使用以下命令: ``` sudo bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip ``` 4.安装完成后,重启Elasticsearch以使IK分词器生效: ``` sudo systemctl restart elasticsearch ``` 5.现在您可以在Elasticsearch索引中使用中文分词IK了。您可以使用以下代码段在索引映射中配置IK分词器: ``` "analysis": { "analyzer": { "ik_max_word": { "tokenizer": "ik_max_word" }, "ik_smart": { "tokenizer": "ik_smart" } }, "tokenizer": { "ik_max_word": { "type": "ik_max_word" }, "ik_smart": { "type": "ik_smart" } } } ``` 在这个示例中,我们为两个分词器ik_max_word和ik_smart定义了令牌器。您可以根据需要添加其他分词器和令牌器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值