01.elasticsearch metric aggregation 查询
文章目錄
- 1. 數(shù)據(jù)準(zhǔn)備
- 2. metric aggregation分類
- 3.使用樣例
- 1 . Avg Aggregation : 求query出來的結(jié)果的average 值
- 2 . Weighted Avg Aggregation: 帶權(quán)重的average值,可以選取另一個字段的值作為權(quán)重值
- 3 . Max Aggregation: 求query出來的結(jié)果的max值
- 4 . Min Aggregation: 求query出來的結(jié)果的min值
- 5 . Sum Aggregation: 求query出來的結(jié)果的sum值
- 6 . Value Count Aggregation: query出來的結(jié)果的某個field的的值的個數(shù),注意可能這個field的值是一個數(shù)組則這個一個doc可能就貢獻(xiàn)了多個value count
- 7 . Cardinality Aggregation: 某個filed的value去重后的count, 可以理解為value_count做了去重
- 8 . Percentiles Aggregation: 百分比求值,對于大小的數(shù)值,求百分位,比如響應(yīng)時間的99分位,95分位等對應(yīng)的具體響應(yīng)時間是多少
- 9 . Percentile Ranks Aggregation: 某個具體的響應(yīng)時間在總體中所處的分位值
- 10. Stats Aggregation: 上面value_count,min,max,sum,avg的快捷方式
- 11. top_hit
elasticsearch的aggregate查詢現(xiàn)在越來越豐富了,目前總共有4類。
本篇就主要學(xué)習(xí)metric aggregation
1. 數(shù)據(jù)準(zhǔn)備
演唱會的票信息
GET seats1028/_search
總共有3w條這樣的數(shù)據(jù)
2. metric aggregation分類
1 . Avg Aggregation : 求query出來的結(jié)果的average 值
2 . Weighted Avg Aggregation: 帶權(quán)重的average值,可以選取另一個字段的值作為權(quán)重值
3 . Max Aggregation: 求query出來的結(jié)果的max值
4 . Min Aggregation: 求query出來的結(jié)果的min值
5 . Sum Aggregation: 求query出來的結(jié)果的sum值
6 . Value Count Aggregation: query出來的結(jié)果的某個field的的值的個數(shù),注意可能這個field的值是一個數(shù)組則這個一個doc可能就貢獻(xiàn)了多個value count
7 . Cardinality Aggregation: 某個filed的value去重后的count, 可以理解為value_count做了去重
8 . Percentiles Aggregation: 百分比求值,對于大小的數(shù)值,求百分位,比如響應(yīng)時間的99分位,95分位等對應(yīng)的具體響應(yīng)時間是多少
9 . Percentile Ranks Aggregation: 某個具體的響應(yīng)時間在總體中所處的分位值
10. Stats Aggregation: 上面value_count,min,max,sum,avg的快捷方式
11. Extended Stats Aggregation: 在stats的基礎(chǔ)上增加了平方和、方差、標(biāo)準(zhǔn)差、平均值加/減兩個標(biāo)準(zhǔn)差的區(qū)間
12. Top Hits Aggregation: 一般是嵌套查詢,用在term查詢當(dāng)中,返回每個bucket的topN
13. Geo Bounds Aggregation: 地理位置的邊界聚合
14. Geo Centroid Aggregation:
15. Scripted Metric Aggregation: 使用script的聚合
16. Median Absolute Deviation Aggregation: 平方差聚合
metric agg可以用作bucket agg的子聚合查詢,但是metric agg 本身不能包含子查詢
3.使用樣例
1 . Avg Aggregation : 求query出來的結(jié)果的average 值
GET seats1028/_search {"size": 0,"aggs": {"avg_price": {"avg": {"field": "price"}}} }返回{"took" : 8,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"avg_price" : {"value" : 4995.498812220319}} }2 . Weighted Avg Aggregation: 帶權(quán)重的average值,可以選取另一個字段的值作為權(quán)重值
GET seats1028/_search {"size": 0,"aggs": {"avg_price": {"weighted_avg": {"value": {"field": "price"},"weight": {"field": "number"}}}} }返回 {"took" : 14,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"avg_price" : {"value" : 4981.713482182667}} }3 . Max Aggregation: 求query出來的結(jié)果的max值
GET seats1028/_search {"size": 0,"aggs": {"max_price": {"max": {"field": "price"}}} } 返回{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"max_price" : {"value" : 9999.0}} }4 . Min Aggregation: 求query出來的結(jié)果的min值
GET seats1028/_search {"size": 0,"aggs": {"min_price": {"min": {"field": "price"}}} }返回{"took" : 0,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"min_price" : {"value" : 0.0}} }5 . Sum Aggregation: 求query出來的結(jié)果的sum值
GET seats1028/_search {"size": 0,"aggs": {"sum_price": {"sum": {"field": "price"}}} }返回{"took" : 8,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"sum_price" : {"value" : 1.80847048E8}} }6 . Value Count Aggregation: query出來的結(jié)果的某個field的的值的個數(shù),注意可能這個field的值是一個數(shù)組則這個一個doc可能就貢獻(xiàn)了多個value count
GET seats1028/_search {"size": 0,"aggs": {"count_price": {"value_count": {"field": "price"}}} }返回 {"took" : 13,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"count_price" : {"value" : 36202}} }7 . Cardinality Aggregation: 某個filed的value去重后的count, 可以理解為value_count做了去重
GET seats1028/_search {"size": 0,"aggs": {"unique_count_price": {"cardinality": {"field": "price"}}} }返回 {"took" : 13,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"unique_count_price" : {"value" : 9591}} }8 . Percentiles Aggregation: 百分比求值,對于大小的數(shù)值,求百分位,比如響應(yīng)時間的99分位,95分位等對應(yīng)的具體響應(yīng)時間是多少
GET seats1028/_search {"size": 0,"aggs": {"percentile_price": {"percentiles": {"field": "price"}}} }返回 {"took" : 56,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"percentile_price" : {"values" : {"1.0" : 100.22,"5.0" : 500.1716280451575,"25.0" : 2478.398741375389,"50.0" : 4990.070945393942,"75.0" : 7509.41570777247,"95.0" : 9487.07620155039,"99.0" : 9894.284278074867}}} }9 . Percentile Ranks Aggregation: 某個具體的響應(yīng)時間在總體中所處的分位值
GET seats1028/_search {"size": 0,"aggs": {"rank_price": {"percentile_ranks": {"field": "price","values": [1000,2500]}}} }返回 {"took" : 15,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"rank_price" : {"values" : {"1000.0" : 9.948376390210644,"2500.0" : 25.172259605722964}}} }10. Stats Aggregation: 上面value_count,min,max,sum,avg的快捷方式
GET seats1028/_search {"size": 0,"aggs": {"stats_price": {"stats": {"field": "price"}}} }返回 {"took" : 9,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 10000,"relation" : "gte"},"max_score" : null,"hits" : [ ]},"aggregations" : {"stats_price" : {"count" : 36202,"min" : 0.0,"max" : 9999.0,"avg" : 4995.498812220319,"sum" : 1.80847048E8}} }11. top_hit
這個一般用于嵌套的子查詢,比如下面的查詢按照row劃分bucket,然后找出每個bucket中的price最高的兩個
GET seats1105/_search {"size": 1,"query": {"match_all": {}},"aggs": {"row_term": {"terms": {"field": "row","size": 10},"aggs": {"top_price": {"top_hits": {"size": 2,"sort": [{"price": {"order": "desc"}}]}}}}} }返回
"aggregations" : {"row_term" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 2,"doc_count" : 5796,"top_price" : {"hits" : {"total" : {"value" : 5796,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "seats1105","_type" : "_doc","_id" : "1957","_score" : null,"_source" : {"price" : 9998},"sort" : [9998]},{"_index" : "seats1105","_type" : "_doc","_id" : "7105","_score" : null,"_source" : {"price" : 9997},"sort" : [9997]}]}}},{"key" : 3,"doc_count" : 5796,"top_price" : {"hits" : {"total" : {"value" : 5796,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "seats1105","_type" : "_doc","_id" : "3993","_score" : null,"_source" : {"price" : 9999},"sort" : [9999]},{"_index" : "seats1105","_type" : "_doc","_id" : "6656","_score" : null,"_source" : {"price" : 9999},"sort" : [9999]}]}}},{"key" : 1,"doc_count" : 5791,"top_price" : {"hits" : {"total" : {"value" : 5791,"relation" : "eq"},"max_score" : null,"hits" : [{"_index" : "seats1105","_type" : "_doc","_id" : "4321","_score" : null,"_source" : {"price" : 9999},"sort" : [9999]},{"_index" : "seats1105","_type" : "_doc","_id" : "8380","_score" : null,"_source" : {"price" : 9999},"sort" : [9999]}]}}}]}總結(jié)
以上是生活随笔為你收集整理的01.elasticsearch metric aggregation 查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 02.pipeline常用process
- 下一篇: 02.elasticsearch buc