第一步:创建索引
创建索引时分词类型text的字段指定IK分词器
{
"mappings":{
"log_type":{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"birthday":{
"type": "date"
},
"desc":{
"type":"text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
} ,
"content":{
"type":"text",
"analyzer":"ik_smart",
"search_analyzer":"ik_smart"
}
}
}
}
}
注意:只是在text类型的字段加入了分词器,keyword字段没有添加(因为keyword是作为一个整体查询的)
确保:建立索引用的分词器和搜索的分词器是一致的("analyzer, "search_analyzer"使用相同分词器)
第二步:插入测试数据
{"index":{}}
{"name":"小黑","age":23,"birthday":"2012-12-12","desc":"事发时份额","content":"默认ES中采用标准分词器进行分词,这种方式并不适用于中文网站,因此需要修改ES对中文友好分词,从而达到更佳的搜索的效果"}
{"index":{}}
{"name":"王小黑","age":24,"birthday":"2012-12-13","desc":"e算法杀神风","content":"默认的搜索的效果"}
{"index":{}}
{"name":"张小五","age":8,"birthday":"2012-12-14","desc":"e算法杀神舒服舒服风","content":"默认舒服舒"}
{"index":{}}
{"name":"win7","age":9,"birthday":"2012-12-15","desc":"不舒服时","content":"默认舒暗示法神鼎飞丹砂服舒"}
{"index":{}}
{"name":"梅超风","age":43,"birthday":"2012-12-16","desc":"风味儿","content":"离开杰弗里斯"}
{"index":{}}
{"name":"张无忌","age":59,"birthday":"2012-12-17","desc":"微服务","content":"变电所发生"}
请求头:
GET 192.168.0.101:9200/log_index/log_type/_search
请求体:
{
"query":{
"term":{
"content":"默认"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"fields": {
"*":{}
}
}
}