提问要多花一点心思哦

您好,想请教您一个高亮搜索,同时展示关联字段的问题

Elasticsearch | 作者 AuroraLove | 发布于2019年02月26日 | 阅读数:3332

我在检索数组元素的时候,为了确定数组元素高亮的位置,需要取到高亮字段所在数组对应的管理id,但是现在不知道如何去取
00{HR366VS9WA_LGN(_~9CV.png

我先根据attributes.value去搜索,对这个字段设置高亮,同时想在highlight中展示所在数组位置的fid
123.png

想找到类似这样的效果
9{@Q1I(SF5G_(Z2_@@@TB_C.png

 
我的DSL
{
"query": {
"match":{
"attributes.value":"火车站"
}
},
"size":1,
"highlight": {
"require_field_match":"false",
"pre_tags": [
"<em>"
],
"post_tags": [
"</em>"
],
"fields": {
"attributes.value": {},
"attributes.id": {},
"attributes":{}
}
}
}





RG{CCPZ4SOA~M7WZDKCDSTB.png
已邀请:

rochy - rochy_he

赞同来自: AuroraLove

貌似没有什么好的方法
推荐更改数据结构

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

赞同来自: AuroraLove

可以实现,参考如下:
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"data": {
"type": "nested"
}
}
}
}
}



POST my_index/_doc/1
{
"data": [
{
"grade": 1,
"comment": " This is test program"
},
{
"grade": 2,
"comment": " This is test program"
},
{
"grade": 3,
"comment": " This is commercial program"
}
]
}


# 2 inner_hits能满足你的要求
POST my_index/_search
{
"query": {
"nested": {
"path": "data",
"query": {
"match": {
"data.comment": "commercial"
}
},
"inner_hits": {}
}
},
"highlight": {
"fields": {
"data.comment": {
"number_of_fragments": 0
}
},
"pre_tags": [
"<em>"
],
"post_tags": [
"</em>"
]
}
}

返回结果:
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"data" : [
{
"grade" : 1,
"comment" : " This is test program"
},
{
"grade" : 2,
"comment" : " This is test program"
},
{
"grade" : 3,
"comment" : " This is commercial program"
}
]
},
"highlight" : {
"data.comment" : [
"This is <em>commercial</em> program"
]
},
"inner_hits" : {
"data" : {
"hits" : {
"total" : 1,
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_nested" : {
"field" : "data",
"offset" : 2
},
"_score" : 0.9808292,
"_source" : {
"grade" : 3,
"comment" : " This is commercial program"
}
}
]
}
}
}
}
]
}
}

要回复问题请先登录注册