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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

阶梯分组

發布時間:2024/4/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阶梯分组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

劃分桶的其它方式

前面講了,劃分桶的方式有很多,例如:

  • Date Histogram Aggregation:根據日期階梯分組,例如給定階梯為周,會自動每周分為一組

  • Histogram Aggregation:根據數值階梯分組,與日期類似

  • Terms Aggregation:根據詞條內容分組,詞條內容完全匹配的為一組

  • Range Aggregation:數值和日期的范圍分組,指定開始和結束,然后按段分組

剛剛的案例中,我們采用的是Terms Aggregation,即根據詞條劃分桶。

接下來,我們再學習幾個比較實用的:

階梯分桶Histogram

原理:

histogram是把數值類型的字段,按照一定的階梯大小進行分組。你需要指定一個階梯值(interval)來劃分階梯大小。

舉例:

比如你有價格字段,如果你設定interval的值為200,那么階梯就會是這樣的:

0,200,400,600,...

上面列出的是每個階梯的key,也是區間的啟點。

如果一件商品的價格是450,會落入哪個階梯區間呢?計算公式如下:

bucket_key = Math.floor((value - offset) / interval) * interval + offset

value:就是當前數據的值,本例中是450

offset:起始偏移量,默認為0

interval:階梯間隔,比如200

因此你得到的key = Math.floor((450 - 0) / 200) * 200 + 0 = 400

操作一下:

比如,我們對汽車的價格進行分組,指定間隔interval為5000:

GET /cars/_search {"size":0,"aggs":{"price":{"histogram": {"field": "price","interval": 5000}}} }

結果:

{"took": 21,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 0,"hits": []},"aggregations": {"price": {"buckets": [{"key": 10000,"doc_count": 2},{"key": 15000,"doc_count": 1},{"key": 20000,"doc_count": 2},{"key": 25000,"doc_count": 1},{"key": 30000,"doc_count": 1},{"key": 35000,"doc_count": 0},{"key": 40000,"doc_count": 0},{"key": 45000,"doc_count": 0},{"key": 50000,"doc_count": 0},{"key": 55000,"doc_count": 0},{"key": 60000,"doc_count": 0},{"key": 65000,"doc_count": 0},{"key": 70000,"doc_count": 0},{"key": 75000,"doc_count": 0},{"key": 80000,"doc_count": 1}]}} }

你會發現,中間有大量的文檔數量為0 的桶,看起來很丑。

我們可以增加一個參數min_doc_count為1,來約束最少文檔數量為1,這樣文檔數量為0的桶會被過濾

示例:

GET /cars/_search {"size":0,"aggs":{"price":{"histogram": {"field": "price","interval": 5000,"min_doc_count": 1}}} }

結果:

{"took": 15,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 0,"hits": []},"aggregations": {"price": {"buckets": [{"key": 10000,"doc_count": 2},{"key": 15000,"doc_count": 1},{"key": 20000,"doc_count": 2},{"key": 25000,"doc_count": 1},{"key": 30000,"doc_count": 1},{"key": 80000,"doc_count": 1}]}} }

完美,!

如果你用kibana將結果變為柱形圖,會更好看:

范圍分桶range

范圍分桶與階梯分桶類似,也是把數字按照階段進行分組,只不過range方式需要你自己指定每一組的起始和結束大小。

總結

以上是生活随笔為你收集整理的阶梯分组的全部內容,希望文章能夠幫你解決所遇到的問題。

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