1. 安装
IK在github上的工程IK Analysis for Elasticsearch,安装IK分词器(进入ES的bin
目录),以目前学习的版本6.4为例:
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
注:如果版本ES版本不一样,那IK分析器也不同,只需要将上述命令中的6.4.0
改成对应的版本号即可(只支持5.5.1+版本)。
2. IK Analyzer
IK Analysis插件支持内置的分析器Analyzer有:ik_smart
和ik_max_word
,内置的标记过生成器Tokenizer有:ik_smart
和ik_max_word
:
ik_smart
:会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,共和国,共和,国,国歌”,穷尽各种可能的组合(在6.4.0版本不会穷尽各种可能);ik_max_word
:会将文本做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”;
在第一步中安装IK分词器完毕后可以调用其中的Analyzer或Tokenizer进行验证,如:
curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'
{
"analyzer": "ik_smart",
"text": "中华人民共和国国歌"
}
'
// 测试结果
{
"tokens": [
{
"token": "中华人民共和国",
"start_offset": 0,
"end_offset": 7,
"type": "CN_WORD",
"position": 0
},
{
"token": "国歌",
"start_offset": 7,
"end_offset": 9,
"type": "CN_WORD",
"position": 1
}
]
}
下面是一些github上的栗子:
// 1. 创建测试索引
curl -XPUT http://localhost:9200/index
// 2. 创建结构化数据
curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
// 指定content字段为ik_max_word分析器
"analyzer": "ik_max_word",
// 指定搜索分析器为ik_max_word分析器
"search_analyzer": "ik_max_word"
}
}
}'
// 3. 在索引中创建一些文档
curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
// 4. 测试搜索
curl -XPOST http://localhost:9200/index/fulltext/_search -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
// 5. 结果
{
"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>渔船 "
]
}
}
]
}
}
3. 配置字典
IK分词器的字典配置文件为%ESHomeDir%\config\analysis-ik\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"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
注:测试时,建议不要直接调用Analyzer或Tokenizer,而是通过像上述创建索引与一些字段绑定的方式进行测试。
4. 热更新IK分词
IK Analysis当前支持热更新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
文件。