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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高

發(fā)布時(shí)間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 概述
  • Terms Aggregation官方文檔
  • 案例一 : 統(tǒng)計(jì)哪種顏色電視銷量最高
    • 模擬數(shù)據(jù)
    • 統(tǒng)計(jì)哪種顏色的電視銷量最高
    • size 參數(shù) 示例
      • 外層size
      • terms節(jié)點(diǎn)下的size


概述

繼續(xù)跟中華石杉老師學(xué)習(xí)ES,第32篇

課程地址: https://www.roncoo.com/view/55


Terms Aggregation官方文檔

官方文檔: 戳這里

詳細(xì)說(shuō)明,參考官網(wǎng)即可,下面我們用示例來(lái)演示下


案例一 : 統(tǒng)計(jì)哪種顏色電視銷量最高

模擬數(shù)據(jù)

創(chuàng)建索引

PUT /tvs {"mappings": {"sales": {"properties": {"price": {"type": "long"},"color": {"type": "keyword"},"brand": {"type": "keyword"},"sold_date": {"type": "date"}}}} }

模擬一批數(shù)據(jù)

POST /tvs/sales/_bulk { "index": {}} { "price" : 1000, "color" : "紅色", "brand" : "長(zhǎng)虹", "sold_date" : "2016-10-28" } { "index": {}} { "price" : 2000, "color" : "紅色", "brand" : "長(zhǎng)虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 3000, "color" : "綠色", "brand" : "小米", "sold_date" : "2016-05-18" } { "index": {}} { "price" : 1500, "color" : "藍(lán)色", "brand" : "TCL", "sold_date" : "2016-07-02" } { "index": {}} { "price" : 1200, "color" : "綠色", "brand" : "TCL", "sold_date" : "2016-08-19" } { "index": {}} { "price" : 2000, "color" : "紅色", "brand" : "長(zhǎng)虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 8000, "color" : "紅色", "brand" : "三星", "sold_date" : "2017-01-01" } { "index": {}} { "price" : 2500, "color" : "藍(lán)色", "brand" : "小米", "sold_date" : "2017-02-12" }

原始數(shù)據(jù):


統(tǒng)計(jì)哪種顏色的電視銷量最高

DSL

GET /tvs/sales/_search {"size": 0,"aggs": {"popular_colors": {"terms": {"field": "color"}}} }

解讀:

  • size:只獲取聚合結(jié)果,而不要執(zhí)行聚合的原始數(shù)據(jù)
  • aggs:固定語(yǔ)法,要對(duì)一份數(shù)據(jù)執(zhí)行分組聚合操作
  • popular_colors:就是對(duì)每個(gè)aggs,都要起一個(gè)名字,自定義,叫啥都行
  • terms:根據(jù)字段的值進(jìn)行分組
  • field:根據(jù)指定的字段的值進(jìn)行分組

類比官方介紹

返回結(jié)果:

  • hits.hits:我們指定了size是0,所以hits.hits就是空的,否則會(huì)把執(zhí)行聚合的那些原始數(shù)據(jù)給你返回回來(lái)
  • aggregations:聚合結(jié)果
  • popular_color:我們指定的某個(gè)聚合的名稱
  • buckets:根據(jù)我們指定的field劃分出的buckets
  • key:每個(gè)bucket對(duì)應(yīng)的那個(gè)值
  • doc_count:這個(gè)bucket分組內(nèi),有多少個(gè)數(shù)據(jù)

類比官網(wǎng)說(shuō)明

每種顏色對(duì)應(yīng)的bucket中的數(shù)據(jù)的數(shù)量,其實(shí)就是這種顏色的銷量

默認(rèn)的排序規(guī)則:按照doc_count降序排序


size 參數(shù) 示例

外層size

當(dāng)我們 外層不加size的時(shí)候,會(huì)返回執(zhí)行聚合的那些原始數(shù)據(jù)

GET /tvs/sales/_search {"aggs": {"popular_color": {"terms": {"field": "color"}}} }

返回

{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 1,"hits": [{"_index": "tvs","_type": "sales","_id": "QzGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 2000,"color": "紅色","brand": "長(zhǎng)虹","sold_date": "2016-11-05"}},{"_index": "tvs","_type": "sales","_id": "PzGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 2000,"color": "紅色","brand": "長(zhǎng)虹","sold_date": "2016-11-05"}},{"_index": "tvs","_type": "sales","_id": "QDGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 3000,"color": "綠色","brand": "小米","sold_date": "2016-05-18"}},{"_index": "tvs","_type": "sales","_id": "QjGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 1200,"color": "綠色","brand": "TCL","sold_date": "2016-08-19"}},{"_index": "tvs","_type": "sales","_id": "RDGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 8000,"color": "紅色","brand": "三星","sold_date": "2017-01-01"}},{"_index": "tvs","_type": "sales","_id": "PjGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 1000,"color": "紅色","brand": "長(zhǎng)虹","sold_date": "2016-10-28"}},{"_index": "tvs","_type": "sales","_id": "QTGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 1500,"color": "藍(lán)色","brand": "TCL","sold_date": "2016-07-02"}},{"_index": "tvs","_type": "sales","_id": "RTGrtGwBCp8vhw_gCmb9","_score": 1,"_source": {"price": 2500,"color": "藍(lán)色","brand": "小米","sold_date": "2017-02-12"}}]},"aggregations": {"popular_color": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "紅色","doc_count": 4},{"key": "綠色","doc_count": 2},{"key": "藍(lán)色","doc_count": 2}]}} }

當(dāng)把外層的 size設(shè)置為1 ,返回1條執(zhí)行聚合的那些原始數(shù)據(jù)

設(shè)置為0 ,不返回執(zhí)行聚合的那些原始數(shù)據(jù)

terms節(jié)點(diǎn)下的size

返回了bucket 中1條數(shù)據(jù)。

. 不設(shè)置時(shí),返回全部的聚合結(jié)果 。

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。