继续跟中华石杉老师学习ES,第62篇
课程地址: https://www.roncoo.com/view/55
7.3版本Highlighting: 戳这里
6.4版本: 戳这里
为了演示这个功能,我们新建个索引
#新建artisan_index
PUT /artisan_index
{
"mappings": {
"artisan_type": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
#写入2条数据
PUT /artisan_index/artisan_type/1
{
"title": "小工匠学习ES",
"content": "小工匠的学习之旅!"
}
PUT /artisan_index/artisan_type/2
{
"title": "我是小工匠",
"content": "欢迎大家"
}
使用highlight查询
GET /artisan_index/artisan_type/_search
{
"query": {
"match": {
"title": "小工匠"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
返回
会变成红色,所以说你的指定的field中,如果包含了那个搜索词的话,就会在那个field的文本中,对搜索词进行红色的高亮显示。
第二个例子: 查询多个字段
GET /blog_website/blogs/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "博客"
}
},
{
"match": {
"content": "博客"
}
}
]
}
},
"highlight": {
"fields": {
"title": {},
"content": {}
}
}
}
highlight中的field,必须跟query中的field一一对齐的
总结:
GET /artisan_index/artisan_type/_search
{
"query": {
"match": {
"content": "小工匠"
}
},
"highlight": {
"pre_tags": [""],
"post_tags": [""],
"fields": {
"content": {
"type": "plain"
}
}
}
}
GET /_search
{
"query" : {
"match": { "user": "kimchy" }
},
"highlight" : {
"fields" : {
"content" : {"fragment_size" : 150, "number_of_fragments" : 3, "no_match_size": 150 }
}
}
}