行动是治愈恐惧的良药,而犹豫、拖延将不断滋养恐惧。

ES 高亮查询仅在 highlight 字段返回标签,如何设置成 _source 也包含呢?

Elasticsearch | 作者 Huey | 发布于2021年04月25日 | 阅读数:1617

如题,当文档包含数组字段,高亮查询时 highlight 中的内容无法知道具体是哪些元素匹配了。
 
实际高亮查询返回的内容:
{
'_source': {
'title': 'Foo bar foobar foo barbar',
'headers': 'HTTP/1.0 200 OK
Server: GeoHttpServer
Date: Thu, 18 Jun 2015 19:02:31 GMT
Content-type: text/html',
'city': 'US'
}
}

期望的返回内容:
{
'_source': {
'title': 'Foo bar foobar <em>foo</em> barbar',
'headers': 'HTTP/1.0 200 OK
Server: GeoHttpServer
Date: Thu, 18 Jun 2015 19:02:31 GMT
Content-type: text/html',
'city': 'US'
}
'highlight': [{'title': "Foo bar foobar <em>foo</em> barbar"}]
}
已邀请:

FFFrp

赞同来自:

highlight 结果不是有字段信息吗

Huey

赞同来自:

比如先存放了这样一个文档,一个订单包含多个订单项。
POST /orders/_doc
{
"create_time": "2021-04-25 22:58:30",
"items": [
{
"goods": "手机",
"count": 1
},
{
"goods": "充电器",
"count": 2
},
{
"goods": "耳机",
"count": 1
}
]
}
高亮查询:
GET /orders/_search
{
"query": {
"match": {
"items.goods": "机"
}
},
"highlight": {
"fields": {
"items.goods": {}
}
}
}
这个查询返回的结果是这样的:
...
"highlight" : {
"items.goods" : [
"手<em>机</em>",
"耳<em>机</em>"
]
}
...

要回复问题请先登录注册