从Elasticsearch返回带有子文档的父数据 [英] Return parent data with child document from Elasticsearch
问题描述
可以在Elasticsearch查询中搜索子文档来返回父数据吗?
Is is possible to return parent data with a search for child documents within an Elasticsearch query?
我有两种文档类型,例如书和章,与父/子相关(不嵌套)。
I have two document types, e.g. Book and Chapter, that are related as Parent/Child (not nested).
我想在子文档上运行搜索,并返回子文档,其中包含父文档中的一些字段。我试图避免对父母执行单独的查询。
I want to run a search on the child document and return the child document, with some of the fields from the parent document. I'm trying to avoid executing a separate query on the parent.
更新
我可以找到的唯一方法是使用 has_child
查询,然后使用一系列的聚合方式来回溯到子级并重新应用查询/过滤器。但是,这似乎过于复杂和低效。
The only way possible I can find is to use the has_child
query and then a series of aggregations to drill back to the children and apply the query/filter again. However, this seems overly complicated and inefficient.
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
}
}
}
}
}
}
}
}
}
}
推荐答案
可以做一个 has_child
查询要返回父文档,使用顶部命中
聚合返回子文档,但这有点麻烦。
It is possible do a has_child
query to return the parent docs with a top hits
aggregation to return the child docs, but it is a bit cumbersome.
Inner Hits
将在1.5.0中发布的功能将会做你想要的。
The Inner Hits
feature that is due to be released in 1.5.0 will do what you want.
http://www.elasticsearch.org/guide /en/elasticsearch/reference/1.x/search-request-inner-hits.html
你可以从主人那里建立源码,然后尝试一下。
You could build the source from master and try it out.
这篇关于从Elasticsearch返回带有子文档的父数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!