0
以下是我的文檔結構ElasticSearch聚集在嵌套場瓢潑大雨對父ID
'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數組那麼我們將如何實現這個 情況一樣嗎?
我使用elasticsearch> = 5.0