注: elasticsearch 版本5.5.2
本篇采用下载源码的方式去下载IK分词器 因为以后如果要进行修改IK分词器可以修改完源码自己进行打包安装
第一步 下载IK分词器
打开github->搜索ik->选择 medcl/elasticsearch-analysis-ik
如图所示,选择和elsticsearch 匹配的版本
下载到本地
第二步 解压并打包
①解压 elasticsearch-analysis-ik-5.5.2.zip
打开eclispe导入 maven项目
②导入完成后
③打包
执行完成后
在target文件夹上右键 选择 Refresh 如图所示
红圈里面的就打包好的ik分词器
第三步 上传并解压
将打包好的elasticsearch-analysis-ik-5.5.2.zip 上传的服务器 并解压到
重启elasticsearch 即可加载ik分词器
第四步 测试
1.创建一个索引
curl -XPUT http://localhost:9200/index
2.创建一个映射
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}'
3.索引一些文档,可以认为就是插入一些信息
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.查询,结果以高亮显示
查询
curl -XPOST http://localhost:9200/index/fulltext/_search -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
结果
{
"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>渔船 "
]
}
}
]
}
}