【发布时间】:2018-12-14 07:33:31
【问题描述】:
是否可以通过在 Elasticsearch 查询中搜索子文档来返回父数据?
我有两种文档类型,例如书和章节,作为父/子相关(非嵌套)。
我想对子文档进行搜索并返回子文档以及父文档中的一些字段。我试图避免对父级执行单独的查询。
更新
我能找到的唯一方法是使用has_child 查询,然后使用一系列聚合来回溯到子级并再次应用查询/过滤器。然而,这似乎过于复杂和低效。
GET index/_search
{
"size": 10,
"query": {
"has_child": {
"type": "chapter",
"query": {
"term": {
"field": "value"
}
}
}
},
"aggs": {
"name1": {
"terms": {
"size": 50,
"field": "id"
},
"aggs": {
"name2": {
"top_hits": {
"size": 50
}
},
"name3": {
"children": {
"type": "type2"
},
"aggs": {
"docFilter": {
"filter": {
"query": {
"match": {
"_all": "value"
}
}
},
"aggs": {
"docs": {
"top_hits": {
"size": 50
}
}
}
}
}
}
}
}
}
}
【问题讨论】:
-
您需要更具体。为
nested查询返回的文档确实包含“根”文档中的字段。 -
我已经用更多信息更新了原始问题。
-
我想知道您是否找到了答案。我正在寻找完全相同的查询。
标签: elasticsearch