一、ik分词简介
ik中包含两种分析器/分词器:
Analyzer: ik_smart , ik_max_word
Tokenizer: ik_smart , ik_max_word
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
二、IK分词的安装
(1)使用IK安装包安装ik分词
下载ik安装包
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.0.0/elasticsearch-analysis-ik-5.0.0.zip
解压到文件夹:
your-es-root/plugins/ik
(2)使用IK源码安装IK
1.下载ik源码包 zip,解压到某个文件夹
2.使用cmd中cd到该文件夹
3.然后执行:
mvn package
4.复制并解压 target/releases/elasticsearch-analysis-ik-{version}.zip 到 your-es-root/plugins/ik
5.重启elasticsearch
附:maven安装
maven主页
http://maven.apache.org/download.cgi
1.maven下载地址
http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
2.解压到C:\java\maven
3.新建环境变量:MAVEN_HOME,设置为:C:\java\maven
修改环境变量:path,添加:%MAVEN_HOME%\bin;
三、ik分词的词库配置
修改配置文件IKAnalyzer.cfg.xml,在如下位置中找:
{conf}/analysis-ik/config/IKAnalyzer.cfg.xml
{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分词
ES默认的分词器为standard, 想要改变默认分词器为ik分词器, 可以在config/elasticsearch.yml文件中添加如下配置即可:
index.analysis.analyzer.default.type:ik_smart
创建mapping
PUT /testindex/_mapping/testtable
{
"testtable": {
"_all": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"term_vector": "no",
"store": "false"
},
"properties": {
"demo": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart",
"include_in_all": "true",
"boost": 8
}
}
}
}
参考:
ik源代码及安装
https://my.oschina.net/xiaohui249/blog/232784
http://blog.csdn.net/yusewuhen/article/details/50685685
分析器的使用及调试
http://www.tuicool.com/articles/eUJJ3qF