2017-07-19 58 views
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

回答

0

我有一個想法,但我不認爲對「ORDER_ID」領域中的最好的之一..

你可以做一個條款聚集,然後「Product.product_category」上的子集聚合。

財產以後這樣的:

{

「AGGS」:{

"all-order-id": { 
    "terms": { 
    "field": "order_id", 
    "size": 10 
    }, 
    "aggs": { 
    "Product-nested": { 
     "nested": { 
     "path": "Product" 
     }, 
     "aggs": { 
     "all-products-in-order-id": { 
      "terms": { 
      "field": "Product.product_category" 
      } 
     } 
     } 
    } 
    } 
} 

}}

對不起它鎖有點亂我不那麼這個答案好編輯