日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

01.elasticsearch metric aggregation 查询

發布時間:2024/2/28 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 01.elasticsearch metric aggregation 查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 數據準備
    • 2. metric aggregation分類
    • 3.使用樣例
      • 1 . Avg Aggregation : 求query出來的結果的average 值
      • 2 . Weighted Avg Aggregation: 帶權重的average值,可以選取另一個字段的值作為權重值
      • 3 . Max Aggregation: 求query出來的結果的max值
      • 4 . Min Aggregation: 求query出來的結果的min值
      • 5 . Sum Aggregation: 求query出來的結果的sum值
      • 6 . Value Count Aggregation: query出來的結果的某個field的的值的個數,注意可能這個field的值是一個數組則這個一個doc可能就貢獻了多個value count
      • 7 . Cardinality Aggregation: 某個filed的value去重后的count, 可以理解為value_count做了去重
      • 8 . Percentiles Aggregation: 百分比求值,對于大小的數值,求百分位,比如響應時間的99分位,95分位等對應的具體響應時間是多少
      • 9 . Percentile Ranks Aggregation: 某個具體的響應時間在總體中所處的分位值
      • 10. Stats Aggregation: 上面value_count,min,max,sum,avg的快捷方式
      • 11. top_hit

elasticsearch的aggregate查詢現在越來越豐富了,目前總共有4類。

  • metric aggregation: 主要是min,max,avg,sum,percetile 等單個統計指標的查詢,同時,可以用作bucket agg的子聚合查詢,但是本身不能包含子查詢
  • bucket aggregation: 主要是類似group by的查詢操作,而且可以含有子查詢
  • matrix aggregation: 使用多個字段的值進行計算從而產生一個多維矩陣
  • pipline aggregation: 主要是能夠在其他的aggregation進行一些附加的處理來增強數據
  • 本篇就主要學習metric aggregation

    1. 數據準備

    演唱會的票信息
    GET seats1028/_search

    { "play" : "Auntie Jo", # 演唱會名稱 "date" : "2018-11-6", # 時間 "theatre" : "Skyline", # 地點 "sold" : false, # 這個票是否已經賣出 "actors" : [ # 演員"Jo Hangum","Jon Hittle","Rob Kettleman","Laura Conrad","Simon Hower","Nora Blue"], "number" : 8, #可以使用的人數(團體座位) "datetime" : 1541497200000, "price" : 8321, # 票價 "tip" : 17.5, # 優惠 "time" : "5:40PM" }

    總共有3w條這樣的數據

    2. metric aggregation分類

    1 . Avg Aggregation : 求query出來的結果的average 值
    2 . Weighted Avg Aggregation: 帶權重的average值,可以選取另一個字段的值作為權重值
    3 . Max Aggregation: 求query出來的結果的max值
    4 . Min Aggregation: 求query出來的結果的min值
    5 . Sum Aggregation: 求query出來的結果的sum值
    6 . Value Count Aggregation: query出來的結果的某個field的的值的個數,注意可能這個field的值是一個數組則這個一個doc可能就貢獻了多個value count
    7 . Cardinality Aggregation: 某個filed的value去重后的count, 可以理解為value_count做了去重
    8 . Percentiles Aggregation: 百分比求值,對于大小的數值,求百分位,比如響應時間的99分位,95分位等對應的具體響應時間是多少
    9 . Percentile Ranks Aggregation: 某個具體的響應時間在總體中所處的分位值
    10. Stats Aggregation: 上面value_count,min,max,sum,avg的快捷方式
    11. Extended Stats Aggregation: 在stats的基礎上增加了平方和、方差、標準差、平均值加/減兩個標準差的區間
    12. Top Hits Aggregation: 一般是嵌套查詢,用在term查詢當中,返回每個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出來的結果的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: 帶權重的average值,可以選取另一個字段的值作為權重值

    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出來的結果的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出來的結果的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出來的結果的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出來的結果的某個field的的值的個數,注意可能這個field的值是一個數組則這個一個doc可能就貢獻了多個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: 百分比求值,對于大小的數值,求百分位,比如響應時間的99分位,95分位等對應的具體響應時間是多少

    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: 某個具體的響應時間在總體中所處的分位值

    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]}]}}}]}

    總結

    以上是生活随笔為你收集整理的01.elasticsearch metric aggregation 查询的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。