ElasticSearch聚合查询
生活随笔
收集整理的這篇文章主要介紹了
ElasticSearch聚合查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 聚合分組
- 求和
- 平均值
- 分析每種顏色下每種品牌的平均價格
- 更多的metric學習
- Cardinality(唯一值)
- 查詢+聚合分析
- 查詢聚合+全局聚合 深入聚合數據分析_global bucket:單個品牌與所有品牌銷量對比
- 過濾+聚合:統計價格大于1200的電視平均價格
- 統計最近一個月的平均價格
- 按照每種品牌的平均價格排序
聚合分組
select * from table group by title.keyword
{"size": 0,"aggs": {"group_name": {"terms": {"field": "title.keyword"}}} }解釋:
size:0 表示只展示聚合結果,不展示原始數據
aggs:表示聚合的操作符
group_name:給聚合操作取名
terms:根據字段的值進行分組
field:根據指定的字段值進行分組
求和
按照title分組求和
select sum(price) from table group by title.
平均值
select avg(price) from table group by title.
{"size": 0,"aggs": {"group_name": {"terms": {"field": "title.keyword"},"aggs":{"avg_price":{"avg":{"field":"price"}}}}} }分析每種顏色下每種品牌的平均價格
{"size": 0,"aggs": {"group_name": {"terms": {"field": "color.keyword"},"aggs": {"group_by_brand": {"terms": {"field": "brand.keyword"},"aggs": {"avg_price_by_color": {"avg": {"field": "price"}}}}}}} }更多的metric學習
{"size": 0,"aggs": {"group_name": {"terms": {"field": "color.keyword"},"aggs": {"avg_price": {"avg": {"field": "price"}},"sum_price": {"sum": {"field": "price"}},"max_price": {"max": {"field": "price"}},"min_max": {"min": {"field": "price"}}}}} }輸出:
"took": 66,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"group_name": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "white","doc_count": 2,"max_price": {"value": 5500},"min_max": {"value": 4500},"avg_price": {"value": 5000},"sum_price": {"value": 10000}},{"key": "blue","doc_count": 1,"max_price": {"value": 4000},"min_max": {"value": 4000},"avg_price": {"value": 4000},"sum_price": {"value": 4000}}]}} }一般來說,90%的常見的數據分析的操作,metric,無非就是count,avg,max,min,sum
Cardinality(唯一值)
cardinality 即去重計算,類似sql中 count(distinct),先去重再求和,計算指定field值的種類數。
{"size": 0,"aggs": {"cartinality_gender": {"cardinality": {"field": "city.keyword"}}} }輸出
{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 15,"max_score": 0,"hits": []},"aggregations": {"cartinality_gender": {"value": 15}} }```## stats 一個聚合,輸出多值 ```java {"size": 0,"aggs": {"stats_price": {"stats": {"field": "price"}}} } {"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"stats_price": {"count": 3,"min": 4000,"max": 5500,"avg": 4666.666666666667,"sum": 14000}} }查詢+聚合分析
{"query":{"match":{"title":"神州"}},"aggs":{"sum_price":{"sum":{"field":"price"}}} }輸出
"took": 54,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 0.9983525,"hits": [{"_index": "aggs_index","_type": "_doc","_id": "5","_score": 0.9983525,"_source": {"brand": "huashuo","color": "white","price": 5500,"title": "神州"}},{"_index": "aggs_index","_type": "_doc","_id": "6","_score": 0.8416345,"_source": {"brand": "dell","color": "white","price": 4500,"title": "神州dell"}}]},"aggregations": {"sum_price": {"value": 10000}} }查詢聚合+全局聚合 深入聚合數據分析_global bucket:單個品牌與所有品牌銷量對比
global:就是global bucket,就是將所有數據納入聚合的scope,而不管之前的query
{"query": {"match": {"title": "神州"}},"aggs": {"sum_price": {"sum": {"field": "price"}},"all": {"global": {},"aggs": {"all_sum_price": {"sum": {"field": "price"}}}}} }輸出
{"query": {"match": {"title": "神州"}},"aggs": {"sum_price": {"sum": {"field": "price"}},"all": {"global": {},"aggs": {"all_sum_price": {"sum": {"field": "price"}}}}} }過濾+聚合:統計價格大于1200的電視平均價格
{"size": 0,"query": {"constant_score": {"filter": {"range": {"price": {"gte": 1200}}}}},"aggs": {"avg_price": {"avg": {"field": "price"}}} }統計最近一個月的平均價格
{"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"}}}}} }按照每種品牌的平均價格排序
{"size": 0,"aggs": {"group_by_color": {"terms": {"field": "brand.keyword","order":{"avg_price":"desc"}},"aggs": {"avg_price": {"avg": {"field": "price"}}}}} }總結
以上是生活随笔為你收集整理的ElasticSearch聚合查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载保存】网页提取正文算法汇总
- 下一篇: Appium安装使用总结