一、Versions
ES Version: 2.1.1
IK Version: 1.6.2
说明:ES和IK的版本一定要相互兼容,具体兼容性可以查看下面的git说明
二、Install
- IK的下载:https://github.com/medcl/elasticsearch-analysis-ik
里面附有安装步骤可以参考,本步骤是略作补充和说明 - compile
mvn package
说明:这个使用mvn命令时很可能会出问题,建议使用工具如IDEA等打包
复制 target/releases/elasticsearch-analysis-ik-{version}.zip
toyour-es-root/plugins/ik,将zip包解压
说明:仅将zip包里的文件放到ik文件夹中 - 配置config文件
将项目中config文件下的ik文件夹复制到your-es-root/config -
配置elasticsearch.yml
在elasticsearch.yml文件的最后添加配置(最简单的配置):index :
analysis :
analyzer :
ik :
type: custom
tokenizer: ik_max_word
-
restart ES
三、Quick Example
(以下大部分摘抄github中的步骤,不同的地方会做标红)可以使用chrome的插件postman,非常好用
-
创建index
curl -XPUT http:
//localhost:9200/index
-
创建mapping(此处与github上不同)
curl -XPOST http:
//localhost:9200/index/fulltext/_mapping -d'
{
"fulltext"
: {
"_all"
: {
"analyzer"
:
"ik"
,
"search_analyzer"
:
"ik_max_word"
,
"term_vector"
:
"no"
,
"store"
:
"false"
},
"properties"
: {
"content"
: {
"type"
:
"string"
,
"store"
:
"no"
,
"term_vector"
:
"with_positions_offsets"
,
"analyzer"
:
"ik"
,
"search_analyzer"
:
"ik_max_word"
,
"include_in_all"
:
"true"
,
"boost"
:
8
}
}
}
}
-
添加文档
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"
:
"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
}
'
-
查询
curl -XPOST http:
//localhost:9200/index/fulltext/_search -d'
{
"query"
: {
"term"
: {
"content"
:
"中国"
}},
"highlight"
: {
"pre_tags"
: [
"<tag1>"
,
"<tag2>"
],
"post_tags"
: [
"</tag1>"
,
"</tag2>"
],
"fields"
: {
"content"
: {}
}
}
}
'
-
结果
四、其他
参考github上的相关说明:https://github.com/medcl/elasticsearch-analysis-ik