統計最大年齡html
GET my_person/_search { "size": 0, "aggs": { "age_max": { "max": { "field": "age" } } } }
結果web
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "age_max": { "value": 28 } } }
統計最小年齡elasticsearch
GET my_person/_search { "size": 0, "aggs": { "age_min": { "min": { "field": "age" } } } }
統計平均年齡ide
GET my_person/_search { "size": 0, "aggs": { "age_avg": { "avg": { "field": "age" } } } }
求和網站
GET my_person/_search { "size": 0, "aggs": { "salary_sum": { "sum": { "field": "salary" } } } }
The stats that are returned consist of: min
, max
, sum
, count
and avg
.ui
GET my_person/_search { "size": 0, "aggs": { "salary_stats": { "stats": { "field": "salary" } } } }
結果spa
{ "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "salary_stats": { "count": 4, "min": 4000, "max": 8000, "avg": 5750, "sum": 23000 } } }
相比於stats新增了幾個統計屬性code
GET my_person/_search { "size": 0, "aggs": { "salary_stats": { "extended_stats": { "field": "salary" } } } }
查詢結果htm
{ "took": 13, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "salary_stats": { "count": 4, "min": 4000, "max": 8000, "avg": 5750, "sum": 23000, "sum_of_squares": 141000000, "variance": 2187500, "std_deviation": 1479.019945774904, "std_deviation_bounds": { "upper": 8708.039891549808, "lower": 2791.960108450192 } } } }
查詢薪資分爲幾個等級blog
GET my_person/_search { "size": 0, "aggs": { "salary_class": { "cardinality": { "field": "salary" } } } }
查詢結果
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "salary_class": { "value": 4 } } }
統計包含某一字段的文檔數量
添加兩個文檔
PUT my_person/my_index/5 { "name":"lucy", "age":23 } PUT my_person/my_index/6 { "name":"blue", "age":20 }
查詢全部文檔中包含」salary「文檔數量
GET my_person/_search { "size": 0, "aggs": { "doc_count": { "value_count": { "field": "salary" } } } }
查詢結果
{ "took": 9, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 6, "max_score": 0, "hits": [] }, "aggregations": { "doc_count": { "value": 4 } } }
https://www.elastic.co/guide/cn/elasticsearch/guide/current/percentiles.html
Elasticsearch 提供的另一個近似度量就是 percentiles
百分位數度量。 百分位數展示某以具體百分比下觀察到的數值。例如,第95個百分位上的數值,是高於 95% 的數據總和。
百分位數一般用來找出異常。在(統計學)的正態分佈下,第 0.13 和 第 99.87 的百分位數表明與均值距離三倍標準差的值。任何處於三倍標準差以外的數據一般被認爲是不尋常的,由於它與平均值相差太大。
假設咱們正運行一個龐大的網站,一個很重要的工做是保證用戶請求能獲得快速響應,所以咱們就須要監控網站的延時來判斷響應是否能保證良好的用戶體驗。
在此場景下,一個經常使用的度量方法就是平均響應延時。 但這並非一個好的選擇(儘管很經常使用),由於平均數一般會隱藏那些異常值, 中位數有着一樣的問題。 咱們能夠嘗試最大值,但這個度量會垂手可得的被單個異常值破壞。
依靠如平均值或中位數這樣的簡單度量
加載 99 百分位數時
人吃驚!在上午九點半時,均值只有 75ms。若是做爲一個系統管理員,咱們都不會看他第二眼。 一切正常!但 99 百分位告訴咱們有 1% 的用戶碰到的延時超過 850ms,這是另一幅場景。 在上午4點48時也有一個小波動,這甚至沒法從平均值和中位數曲線上觀察到。