【发布时间】:2017-07-20 05:02:46
【问题描述】:
以下是我的文档结构
'Order': {
u'properties': {
u'order_id': {u'type': u'integer'},
'Product': {
u'properties': {
u'product_id': {u'type': u'integer'},
u'product_category': {'type': 'text'},
},
u'type': u'nested'
}
}
}
文档1
"Order": {
"order_id": "1",
"Product": [
{
"product_id": "1",
"product_category": "category_1"
},
{
"product_id": "2",
"product_category": "category_2"
},
{
"product_id": "3",
"product_category": "category_2"
},
]
}
文档2
"Order": {
"order_id": "2",
"Product": [
{
"product_id": "4",
"product_category": "category_1"
},
{
"product_id": "1",
"product_category": "category_1"
},
{
"product_id": "2",
"product_category": "category_2"
},
]
}
我想得到以下输出
"aggregations": {
"Order": [
{
"order_id": "1"
"category_counts": [
{
"category_1": 1
},
{
"category_2": 2
},
]
},
{
"order_id": "1"
"category_counts": [
{
"category_1": 2
},
{
"category_2": 1
},
]
},
]
}
我尝试使用嵌套聚合
"aggs": {
"Product-nested": {
"nested": {
"path": "Product"
}
"aggs": {
"category_counts": {
"terms": {
"field": "Product.product_category"
}
}
},
}
}
它不会为每个订单提供输出,而是为所有订单提供组合输出
{
"Product-nested": {
"category_counts": [
"category_1": 3,
"category_2": 3
]
}
}
我有两个问题:
- 如何在上述情况下获得所需的输出?
- 如果我有一个数组而不是单个 product_category 怎么办? product_categories 那么我们将如何在此实现相同的目标 场景?
我正在使用弹性搜索 >= 5.0
【问题讨论】:
标签: elasticsearch elasticsearch-aggregation