【发布时间】:2018-02-14 23:52:16
【问题描述】:
目标:对 Parent 类型的文档执行 1 次搜索,并在结果中包含每个父文档的子文档数。
(弹性搜索 v5)
数据模型有 2 种文档类型:Parent 和 Child。
我发现我可以做以下查询:
GET /stack/parent_doc/_search/
{
"query": {
"has_child": {
"type": "child_doc",
"inner_hits": {
"_source": false,
"size": 0
},
"query": {
"match_all": {}
}
}
}
}
我会返回所有至少有一个孩子的父母及其孩子文档的数量,如下所示。这非常接近,但我也希望有没有孩子的父母。
{
"took": 4077,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"failed": 0
},
"hits": {
"total": 4974405,
"max_score": 1,
"hits": [{
"_index": "stack",
"_type": "parent_doc",
"_id": "f34e4848-fd63-35a3-84d3-82cbc8796473",
"_score": 1,
"_source": {
"field": "value"
},
"inner_hits": {
"child_doc": {
"hits": {
"total": 1,
"max_score": 0,
"hits": []
}
}
}
},
{
"_index": "stack",
"_type": "parent_doc",
"_id": "f34e1ece-2274-35f6-af37-37138825db20",
"_score": 1,
"_source": {
"field": "value"
},
"inner_hits": {
"child_doc": {
"hits": {
"total": 5,
"max_score": 0,
"hits": []
}
}
}
}
]
}
}
如果我删除查询的 match_all 部分,那么 ES 似乎完全忽略了 has_child 子句,返回所有 Parent 文档,无论他们是否有孩子(这是我想要的)但没有 @ 987654325@,所以我不明白。
"query": {
"match_all": {}
}
有没有办法在单个查询中做到这一点?
【问题讨论】:
标签: elasticsearch elasticsearch-5