是否可以通过在 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
}
}
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
可以执行has_child查询来返回父文档,并通过top hits聚合返回子文档,但这有点麻烦。
Inner Hits即将在 1.5.0 中发布的功能将满足您的需求。
http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/search-request-inner-hits.html
您可以从 master 构建源代码并尝试一下。
| 归档时间: |
|
| 查看次数: |
10589 次 |
| 最近记录: |