从地址https://github.com/medcl/elasticsearch-analysis-ik下载elasticsearch中文分词器
这里默认的是master的 但是master的项目需要用gradle编译,这里选择1.8.0版本。而且从下面的介绍可以知道1.8.0正好对应elasticsearch的2.2.0版本
下载后的压缩包解压后进去发现是pom工程
分别执行如下命令:
mvn clean
mvn compile
mvn package
当然这里是用maven对此工程进行编译,前提要安装maven
Maven安装也很简单,下载包以后解压缩,配置环境变量即可,如图:
前面编译了插件以后会在target/releases目录下出现一个zip包
在安装好的elasticsearch中在plugins目录下新建ik目录,将此zip包拷贝到ik目录下
将权限修改为elasticsearch启动用户的权限,通过unzip命令解压缩
例如在plugins/ik****目录下执行unzip elasticsearch-analysis-ik-1.8.0.zip
每台机器都这样操作,重新启动elasticsearch集群
注:目前elasticsearch的版本对默认设置ik分词器还是有bug的,网上的设置基本都是有问题的,这里可以在建立索引的时候设置 **
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
"fulltext": {
"properties": {
"content": {
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"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" : { "term" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["", ""],
"post_tags" : [" ", ""],
"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": [
"中国 驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
]
}
},
{
"_index": "index",
"_type": "fulltext",
"_id": "3",
"_score": 2,
"_source": {
"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight": {
"content": [
"均每天扣1艘中国 渔船 "
]
}
}
]
}
}
```