2015-09-16 32 views
0

我在下面的格式如何使用elasticsearch方面查詢GROUPBY結果

{ 
"ID": { "Color": "Black", "Product": "Car" }, 
"ID": { "Color": "Black", "Product": "Car" }, 
"ID": { "Color": "Black", "Product": "Van" }, 
"ID": { "Color": "Black", "Product": "Van" }, 
"ID": { "Color": "Ash", "Product": "Bike" } 
} 

我要計算汽車的數量和相應顏色的JSON數據。我正在使用elasticsearch方面來做到這一點。

我查詢

$http.post('http://localhost:9200/product/productinfoinfo/_search?size=5', { "aggregations": { "ProductInfo": { "terms": { "field": "product" } } }, "facets": { "ProductColor": { "terms": { "field": "Color", "size": 10 } } } }) 

我得到的輸出像下面

"facets": { "ProductColor": { "_type": "terms", "missing": 0, "total": 7115, "other": 1448, "terms": [ { "term": "Black", "count": 4 }, { "term": "Ash","count":1} }, 
"aggregations": { "ProductInfo": { "doc_count_error_upper_bound": 94, "sum_other_doc_count": 11414, "buckets": [ { "key": "Car", "doc_count": 2 }, { "key": "Van", "doc_count": 2 }, { "key": "Bike", "doc_count": 1 } ] } } } 

我真正想要的是,

[ { "key": "Car", "doc_count": 2, "Color":"Black", "count":2 }, { "key": "Van", "doc_count": 2,"Color":"Black", "count":2 }, { "key": "Bike", "doc_count": 1,"Color":"Ash", "count":1 } ] 

我想GROUPBY結果。是否有可能在elasticsearch查詢中做到這一點。

在此先感謝

回答

2

這是因爲您同時使用聚合和方面,其中,如果相似,並不意味着要一起使用。

方面已被棄用,將很快從ElasticSearch中刪除。聚合是進行「group by」類查詢的方式。

你一定要窩在第一彼此terms聚集,像這樣:

{ 
    "aggs": { 
    "By_type": { 
     "terms": { 
     "field": "Product" 
     }, 
     "aggs": { 
     "By_color": { 
      "terms": { 
      "field": "Color" 
      } 
     } 
     } 
    } 
    } 
} 

,你想要什麼結果將接近:

"aggregations": { 
     "By_type": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": "bike", 
       "doc_count": 2, 
       "By_color": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
        { 
         "key": "ash", 
         "doc_count": 1 
        }, 
        { 
         "key": "black", 
         "doc_count": 1 
        } 
        ] 
       } 
      }, 
      { 
       "key": "car", 
       "doc_count": 2, 
       "By_color": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
        { 
         "key": "black", 
         "doc_count": 2 
        } 
        ] 
       } 
      }, 
      { 
       "key": "van", 
       "doc_count": 1, 
       "By_color": { 
        "doc_count_error_upper_bound": 0, 
        "sum_other_doc_count": 0, 
        "buckets": [ 
        { 
         "key": "black", 
         "doc_count": 1 
        } 
        ] 
       } 
      } 
     ] 
     } 
    } 
+0

我們需要使用'」 aggs「或'」aggregation「' – Mangoski

+0

這是完全一樣的東西:你可以同時使用,但我更喜歡使用'aggs',因爲它的輸入時間較短,但使用'aggregations'會產生相同的結果。 – ThomasC

+0

還有一個疑問可以爲groupby添加更多aggs。它會起作用嗎? – Mangoski

相關問題