selectcount(*)
from tvs.sales
where brand like "%長%"
group by price
es aggregation,scope,任何的聚合,都必須在搜索出來的結果數據中之行,搜索結果,就是聚合分析操作的scope
GET /tvs/sales/_search
{"size":0,"query":{"term":{"brand":{"value":"小米"}}},"aggs":{"group_by_color":{"terms":{"field":"color"}}}}{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0,"hits":[]},"aggregations":{"group_by_color":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"綠色","doc_count":1},{"key":"藍色","doc_count":1}]}}}
2. global bucket:單個品牌與所有品牌銷量對比
aggregation,scope,一個聚合操作,必須在query的搜索結果范圍內執行
GET /tvs/sales/_search
{"size":0,"query":{"term":{"brand":{"value":"長虹"}}},"aggs":{"single_brand_avg_price":{"avg":{"field":"price"}},"all":{"global":{},"aggs":{"all_brand_avg_price":{"avg":{"field":"price"}}}}}}
GET /tvs/sales/_search
{"size":0,"query":{"constant_score":{"filter":{"range":{"price":{"gte":1200}}}}},"aggs":{"avg_price":{"avg":{"field":"price"}}}}{"took":41,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":7,"max_score":0,"hits":[]},"aggregations":{"avg_price":{"value":2885.714285714286}}}
4. bucket filter:統計牌品最近一個月的平均價格
GET /tvs/sales/_search
{"size":0,"query":{"term":{"brand":{"value":"長虹"}}},"aggs":{"recent_150d":{"filter":{"range":{"sold_date":{"gte":"now-150d"}}},"aggs":{"recent_150d_avg_price":{"avg":{"field":"price"}}}},"recent_140d":{"filter":{"range":{"sold_date":{"gte":"now-140d"}}},"aggs":{"recent_140d_avg_price":{"avg":{"field":"price"}}}},"recent_130d":{"filter":{"range":{"sold_date":{"gte":"now-130d"}}},"aggs":{"recent_130d_avg_price":{"avg":{"field":"price"}}}}}}
GET /tvs/sales/_search
{"size":0,"aggs":{"group_by_color":{"terms":{"field":"color"},"aggs":{"avg_price":{"avg":{"field":"price"}}}}}}{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":8,"max_score":0,"hits":[]},"aggregations":{"group_by_color":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"紅色","doc_count":4,"avg_price":{"value":3250}},{"key":"綠色","doc_count":2,"avg_price":{"value":2100}},{"key":"藍色","doc_count":2,"avg_price":{"value":2000}}]}}}
GET /tvs/sales/_search
{"size":0,"aggs":{"group_by_color":{"terms":{"field":"color","order":{"avg_price":"asc"}},"aggs":{"avg_price":{"avg":{"field":"price"}}}}}}
6. 顏色+品牌下鉆分析時按最深層metric進行排序
GET /tvs/sales/_search
{"size":0,"aggs":{"group_by_color":{"terms":{"field":"color"},"aggs":{"group_by_brand":{"terms":{"field":"brand","order":{"avg_price":"desc"}},"aggs":{"avg_price":{"avg":{"field":"price"}}}}}}}}
GET /tvs/sales/_search
{"size":0,"aggs":{"months":{"date_histogram":{"field":"sold_date","interval":"month"},"aggs":{"distinct_colors":{"cardinality":{"field":"brand"}}}}}}{"took":70,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":8,"max_score":0,"hits":[]},"aggregations":{"group_by_sold_date":{"buckets":[{"key_as_string":"2016-05-01T00:00:00.000Z","key":1462060800000,"doc_count":1,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2016-06-01T00:00:00.000Z","key":1464739200000,"doc_count":0,"distinct_brand_cnt":{"value":0}},{"key_as_string":"2016-07-01T00:00:00.000Z","key":1467331200000,"doc_count":1,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2016-08-01T00:00:00.000Z","key":1470009600000,"doc_count":1,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2016-09-01T00:00:00.000Z","key":1472688000000,"doc_count":0,"distinct_brand_cnt":{"value":0}},{"key_as_string":"2016-10-01T00:00:00.000Z","key":1475280000000,"doc_count":1,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2016-11-01T00:00:00.000Z","key":1477958400000,"doc_count":2,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2016-12-01T00:00:00.000Z","key":1480550400000,"doc_count":0,"distinct_brand_cnt":{"value":0}},{"key_as_string":"2017-01-01T00:00:00.000Z","key":1483228800000,"doc_count":1,"distinct_brand_cnt":{"value":1}},{"key_as_string":"2017-02-01T00:00:00.000Z","key":1485907200000,"doc_count":1,"distinct_brand_cnt":{"value":1}}]}}}
8. cardinality算法之優化內存開銷以及HLL算法
1. cardinality解析
cardinality,count(distinct),5%的錯誤率,性能在100ms左右
precision_threshold優化準確率和內存開銷
GET /tvs/sales/_search
{"size":0,"aggs":{"distinct_brand":{"cardinality":{"field":"brand","precision_threshold":100}}}}
PUT /tvs/{"mappings":{"sales":{"properties":{"brand":{"type":"text","fields":{"hash":{"type":"murmur3"}}}}}}}
GET /tvs/sales/_search
{"size":0,"aggs":{"distinct_brand":{"cardinality":{"field":"brand.hash","precision_threshold":100}}}}超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生