环境:
centos 7.2
es 6.5.3
ik下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases
在线安装命令:
cd /usr/share/elasticsearch
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.3/elasticsearch-analysis-ik-6.5.3.zip
验证:
kibana:
GET /_analyze
{
"analyzer": "ik_max_word"
, "text": ["中华人民共和国国歌"]
}
GET /_analyze
{
"analyzer": "ik_smart"
, "text": ["中华人民共和国国歌"]
}
创建一个index:
curl -XPUT http://localhost:9200/index
1
定义文档的字段,主要是指定使用IK插件:
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125:9200/index/fulltext/_mapping -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}'
添加一些数据:
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
查询:
curl -H "Content-Type: application/json" -XPOST http://192.168.0.125: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>渔船 "
]
}
}
]
}
}