之前使用Elasticsearch安装head插件成功了,但是安装IK分词器却失败了。貌似是ElasticSearch5.0以后就不支持直接在elasticsearch.yml中配置IK了。原因下面会说明。
首先下载:
https://www.elastic.co/downloads/elasticsearch
https://github.com/medcl/elasticsearch-analysis-ik
,最新版本貌似是5.3 了。
第一步 预处理:
- 解压elasticsearch-5.3.0.zip>D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0
- 解压elasticsearch-analysis-ik-5.3.0>D:\programfiles\softtools\elasticsearch\plugins\elasticsearch-analysis-ik-5.3.0
- 编译elasticsearch-analysis-ik插件 cmd>D:\programfiles\softtools\elasticsearch\plugins\elasticsearch-analysis-ik-5.3.0>mvn clean package
第二步 配置:
-
IK配置,如下图是IK分词器config目录下的配置D:\programfiles\softtools\elasticsearch\plugins\elasticsearch-analysis-ik-5.3.0\config
-
在D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\config目录中新建ik文件夹,把上图中的IK配置文件放入ElasticSearch目录中的D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\config\ik目录下
ik里面的内容就是 第二步:1.IK配置 里面的配置。
3. 配置IK分词器
下图是通过mvn命令编译后生成的zip包
将上图中的zip包放入D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\plugins\ik (ps:ik目录没有的话自己新建一个就好),进行解压
结果如下图所示:
整个安装就完成了。
第三步 测试:
1. 修改elasticsearch.yml中的配置
设置cluster.name,node.name,同时解开这两个配置的注释
设置http.port:9200,其他不需要增加配置了
2. windows下双击D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\bin\elasticsearch.bat
不报错说明安装成功。
3. 安装Http连接访问工具,比如火狐插件httpRequester,安装完成后会在火狐浏览器右上角出现插件的标示。
4. 创建索引
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
点击Put按钮,出现图右边的返回值,说明创建索引成功。
5. 进行分词测试:
安装过程遇到的坑:
1.将网上对使用IK的配置放到elasticsearch.yml中就会报如下的错误:
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalArgumentException: node settings must not contain any index level settings
这个无论怎么想都不明白,而且网上还没有几个人遇到过这种情况。
2.Caused by: java.lang.IllegalStateException: jar hell!
将IK编译出来的jar,和source.jar两个文件放入elasticsearch的lib里后就出现了,有人说是版本问题,但是我选择的两者都是最新版本,并且对应。这里是因为lib里面有IK了,然后elasticsearch去读取plugin文件夹下的插件的时候又装载了一次,于是就出现了这个问题。将lib里的IK jar包删除,改从plugins里加载就没有问题了。网上也有不少是这样做的,但是确实这么做不正确。
在D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\logs会看到相应的错误日志。
3. Exception in thread “main” java.lang.IllegalStateException: Could not load plugin descriptor for existing plugin [analysis-ik]. Was the plugin built before 2.0?
Likely root cause: java.nio.file.NoSuchFileException: /home/es/es2/plugins/analysis-ik/plugin-descriptor.properties
出现上面的异常是我一开始把编译出来的jar包放入D:\programfiles\softtools\elasticsearch\elasticsearch-5.3.0\plugins\ik里面了,其实是要把D:\programfiles\softtools\elasticsearch\plugins\elasticsearch-analysis-ik-5.3.0\target\releases\elasticsearch-analysis-ik-5.3.0.zip解压到那个ik目录下面。
4. 为什么没有在elasticsearch.yml配置IK相关的分词属性呢
5. 在github上 https://github.com/medcl/elasticsearch-analysis-ik的使用文档说了:
5.0.0及其以后就不会上面两个分词器了,而使用两个参数去替代,同时根据上面的使用文档来说,作者也没有说明要针对IK配置elasticsearch.yml。
总体来说还是遇到一定坎坷的,网上很多ES的文章对于新版本都不适用了。我从新将5.0.0的安装环境全部删除然后引入最新版本,才搞定了IK分词器。最新版本的ES安装head插件应该没有什么问题了,毕竟head也没有要求要对5.0.0以后的版本进行安装配置的改动。
Install
1.download or compile
download pre-build package from here: https://github.com/medcl/elasticsearch-analysis-ik/releases
or compiled from the source:
checkout ik version respective to your elasticsearch version
git checkout tags/{version}
mvn package
copy and unzip target/releases/elasticsearch-analysis-ik-{version}.zip
to your-es-root/plugins/ik
2.restart elasticsearch
Tips:
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
例如:
GET /_analyze?pretty&analyzer=ik_max_word
{"text":"中华人民共和国"}
Quick Example
1.create a index
curl -XPUT http://localhost:9200/index
2.create a mapping
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
"fulltext": {
"_all": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"term_vector": "no",
"store": "false"
},
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
3.index some docs
curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
4.query with highlighting
curl -XPOST http://localhost:9200/index/fulltext/_search -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
Result
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2,
"hits": [
{
"_index": "index",
"_type": "fulltext",
"_id": "4",
"_score": 2,
"_source": {
"content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
},
"highlight": {
"content": [
"<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
]
}
},
{
"_index": "index",
"_type": "fulltext",
"_id": "3",
"_score": 2,
"_source": {
"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight": {
"content": [
"均每天扣1艘<tag1>中国</tag1>渔船 "
]
}
}
]
}
}
Dictionary Configuration
IKAnalyzer.cfg.xml
can be located at {conf}/analysis-ik/config/IKAnalyzer.cfg.xml
or {plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>
热更新 IK 分词使用方法
目前该插件支持热更新 IK 分词,通过上文在 IK 配置文件中提到的如下配置
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">location</entry>
其中 location
是指一个 url,比如 http://yoursite.com/getCustomDict
,该请求只需满足以下两点即可完成分词热更新。
-
该 http 请求需要返回两个头部(header),一个是
Last-Modified
,一个是ETag
,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。 -
该 http 请求返回的内容格式是一行一个分词,换行符用
\n
即可。
满足上面两点要求就可以实现热更新分词了,不需要重启 ES 实例。
可以将需自动更新的热词放在一个 UTF-8 编码的 .txt 文件里,放在 nginx 或其他简易 http server 下,当 .txt 文件修改时,http server 会在客户端请求该文件时自动返回相应的 Last-Modified 和 ETag。可以另外做一个工具来从业务系统提取相关词汇,并更新这个 .txt 文件。
have fun.
常见问题
1.自定义词典为什么没有生效?
请确保你的扩展词典的文本格式为 UTF8 编码
2.如何手动安装?
git clone https://github.com/medcl/elasticsearch-analysis-ik
cd elasticsearch-analysis-ik
git checkout tags/{version}
mvn clean
mvn compile
mvn package
拷贝和解压release下的文件: #{project_path}/elasticsearch-analysis-ik/target/releases/elasticsearch-analysis-ik-*.zip 到你的 elasticsearch 插件目录, 如: plugins/ik 重启elasticsearch
3.分词测试失败 请在某个索引下调用analyze接口测试,而不是直接调用analyze接口 如:http://localhost:9200/your_index/_analyze?text=中华人民共和国MN&tokenizer=my_ik
Changes
5.0.0
- 移除名为
ik
的analyzer和tokenizer,请分别使用ik_smart
和ik_max_word
Thanks
YourKit supports IK Analysis for ElasticSearch project with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .NET Profiler.