【ES】學習8-聚合1
阿新 • • 發佈:2017-06-14
target style nsa 搜索 名稱 制造商 tab sea bsp
參考資料:
https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_combining_the_two.html
特定概念:
桶(Buckets)滿足特定條件的文檔的集合
指標(Metrics)對桶內的文檔進行統計計算
SELECT COUNT(color)
FROM table
GROUP BY color
COUNT(color)
相當於指標。
GROUP BY color
相當於桶。
聚合例子:
curl -XGET ‘localhost:9200/text/_search?pretty‘ -d ‘ {"size":0, "aggs": {"popular_ports": {"terms": {"field":"port"} } } }‘
對port字段做聚合,結果名稱為popular_ports。size設為0表示不關心搜索結果,結果的hits部分會是空值。
計算每種顏色車輛價格的平均值
GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
嵌套桶
每個顏色的汽車制造商的分布
GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": {"field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "make": { "terms": { "field": "make" } } } } } }
每個顏色的汽車制造商的分布,以及各個制造商生產汽車的最高和最低價格。
GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "make" : { "terms" : { "field" : "make" }, "aggs" : { "min_price" : { "min": { "field": "price"} }, "max_price" : { "max": { "field": "price"} } } } } } } }
【ES】學習8-聚合1