ELK,萌萌哒

设置字段高亮

Elasticsearch | 作者 qqq1234567 | 发布于2018年10月31日 | 阅读数:2212

现在有两种类型的词,都是用term匹配,一类需要高亮,一类不需要,该怎么差
已邀请:

zz_hello

赞同来自: rochy

  "highlight": {
"fields": {
"title": {
"highlight_query": {
"bool": {
"should": [
{
"term": {
"title": {
"value": "开始"
}
}
}
]
}
}
}
}
}
在highlight里面设置"highlight_query"

zz_hello

赞同来自:

PUT test1031
{
"mappings": {
"zz":{
"properties":{
"title":{
"type":"keyword"
},
"city":{
"type":"keyword"
}
}
}
}
}
PUT test1031/zz/1
{
"title":"上海",
"city":"上海"
}
PUT test1031/zz/2
{
"title":"北京",
"city":"北京"
}

GET test1031/_search
{
"query": {
"term": {
"title": {
"value": "上海"
}
}
},
"highlight": {
"fields": {
"title": {}
}
}
}

GET test1031/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"title": {
"value": "上海"
}
}
},{
"term": {
"city": {
"value": "上海"
}
}
}
]
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
不知道你说的都用term查询是什么样的,所以写了两个查询。结果是一样的,只高亮title
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "test1031",
"_type": "zz",
"_id": "1",
"_score": 0.5753642,
"_source": {
"title": "上海",
"city": "上海"
},
"highlight": {
"title": [
"<em>上海</em>"
]
}
}
]
}
}

qqq1234567

赞同来自:


{
"query": {
"bool": {
"must":
{
"term": {
"title": {
"value": "上海"
}
}
},
"should":{
"terms":{
"title":{
"value":"都是","开始"
}
}
}
}
}
}
像这样,一个字段匹配两次查询,但只高亮下面的should里的terms查询

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

最主要的原因是你检索的字段是title,应该如下处理能满足要求。
POST test1031/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"city": {
"value": "上海"
}
}
},
{
"term": {
"title": {
"value": "上海"
}
}
}
]
}
},
"highlight": {
"fields": {
"title": {},
"city":{}
}
}
}

要回复问题请先登录注册