在 Elasticsearch 中有一个父子结构表示一个order
有order_revision
孩子的我想生成一个直方图,price
显示quantity
.
{
"_type": "order",
"_id": "1063220887",
"_score": 1,
"_source": {
"order_id": "1063220887",
"product_id": "10446350",
"timestamp": 1462713302000
}
}
{
"_type": "order_revision",
"_id": "10234234",
"_parent": "1063220887",
"_source": {
"price": 9,
"quantity": 3,
"revision": 361,
"timestamp": 1462712196000
}
}
以下聚合基本上有效,但返回所有现有修订的总和。
{
"aggs": {
"orders": {
"filter": {
"has_parent": {
"parent_type": "order"
}
},
"aggs": {
"quantity_per_price": {
"histogram": {
"field": "price",
"interval": 1
}
"aggs": {
"sum": {"field": quantity"}
}
}
}
}
}
}
在最终版本中,它应该只返回每个订单的最新版本(最高/最新)的quantity
字段总和。我不完全确定如何提出这样的聚合,它按分组并只选择最新的孩子,我也不确定这个父子结构是否是对这些数据建模的最佳选择。timestamp
order_id