ElasticSearch聚合查询
生活随笔
收集整理的這篇文章主要介紹了
ElasticSearch聚合查询
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 聚合分組
- 求和
- 平均值
- 分析每種顏色下每種品牌的平均價(jià)格
- 更多的metric學(xué)習(xí)
- Cardinality(唯一值)
- 查詢(xún)+聚合分析
- 查詢(xún)聚合+全局聚合 深入聚合數(shù)據(jù)分析_global bucket:單個(gè)品牌與所有品牌銷(xiāo)量對(duì)比
- 過(guò)濾+聚合:統(tǒng)計(jì)價(jià)格大于1200的電視平均價(jià)格
- 統(tǒng)計(jì)最近一個(gè)月的平均價(jià)格
- 按照每種品牌的平均價(jià)格排序
聚合分組
select * from table group by title.keyword
{"size": 0,"aggs": {"group_name": {"terms": {"field": "title.keyword"}}} }解釋:
size:0 表示只展示聚合結(jié)果,不展示原始數(shù)據(jù)
aggs:表示聚合的操作符
group_name:給聚合操作取名
terms:根據(jù)字段的值進(jìn)行分組
field:根據(jù)指定的字段值進(jìn)行分組
求和
按照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"}}}}} }分析每種顏色下每種品牌的平均價(jià)格
{"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學(xué)習(xí)
{"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}}]}} }一般來(lái)說(shuō),90%的常見(jiàn)的數(shù)據(jù)分析的操作,metric,無(wú)非就是count,avg,max,min,sum
Cardinality(唯一值)
cardinality 即去重計(jì)算,類(lèi)似sql中 count(distinct),先去重再求和,計(jì)算指定field值的種類(lèi)數(shù)。
{"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 一個(gè)聚合,輸出多值 ```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}} }查詢(xún)+聚合分析
{"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}} }查詢(xún)聚合+全局聚合 深入聚合數(shù)據(jù)分析_global bucket:單個(gè)品牌與所有品牌銷(xiāo)量對(duì)比
global:就是global bucket,就是將所有數(shù)據(jù)納入聚合的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"}}}}} }過(guò)濾+聚合:統(tǒng)計(jì)價(jià)格大于1200的電視平均價(jià)格
{"size": 0,"query": {"constant_score": {"filter": {"range": {"price": {"gte": 1200}}}}},"aggs": {"avg_price": {"avg": {"field": "price"}}} }統(tǒng)計(jì)最近一個(gè)月的平均價(jià)格
{"size": 0,"query": {"term": {"brand": {"value": "長(zhǎng)虹"}}},"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"}}}}} }按照每種品牌的平均價(jià)格排序
{"size": 0,"aggs": {"group_by_color": {"terms": {"field": "brand.keyword","order":{"avg_price":"desc"}},"aggs": {"avg_price": {"avg": {"field": "price"}}}}} }總結(jié)
以上是生活随笔為你收集整理的ElasticSearch聚合查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【转载保存】网页提取正文算法汇总
- 下一篇: Appium安装使用总结