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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ElasticSearch中distinct,count和group by的实现

發布時間:2025/4/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ElasticSearch中distinct,count和group by的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在業務中需要使用ES來進行數據查詢,在某些場景下需要對數據進行去重,以及去重后的統計。為了方便大家理解,特意從SQL角度,方便大家能夠理解ES查詢語句。

?

1 - distinct

SELECT DISTINCT(user_id) FROM table WHERE user_id_type = 3;

?

{"query": {"term": {"user_id_type": 3}},"collapse": {"field": "user_id"} }

  

{..."hits": {"hits": [{"_index": "es_qd_mkt_visitor_packet_dev_v1_20180621","_type": "ad_crowd","_source": {"user_id": "wx2af8414b502d4ca2_oHtrD0Vxv-_8c678figJNHmtaVQQ","user_id_type": 3},"fields": {"user_id": ["wx2af8414b502d4ca2_oHtrD0Vxv-_8c678figJNHmtaVQQ"]}}]} }

  

總結:使用collapse字段后,查詢結果中[hits]中會出現[fields]字段,其中包含了去重后的user_id

2 - count + distinct

SELECT COUNT(DISTINCT(user_id)) FROM table WHERE user_id_type = 3;

  

{"query": {"term": {"user_id_type": 3}},"aggs": {"count": {"cardinality": {"field": "user_id"}}} }

 

{..."hits": {...},"aggregations": {"count": {"value": 121}} }

  

總結:aggs中cardinality的字段代表需要distinct的字段

3 - count + group by

SELECT COUNT(user_id) FROM table GROUP BY user_id_type;

  

{"aggs": {"user_type": {"terms": {"field": "user_id_type"}}} }

  

{..."hits": {...},"aggregations": {"user_type": {..."buckets": [{"key": 4,"doc_count": 1220},{"key": 3,"doc_count": 488}]}} }

  

總結:aggs中terms的字段代表需要gruop by的字段

4 - count + distinct + group by

SELECT COUNT(DISTINCT(user_id)) FROM table GROUP BY user_id_type;

  

{"aggs": {"user_type": {"terms": {"field": "user_id_type"},"aggs": {"count": {"cardinality": {"field": "user_id"}}}}} } {..."hits": {...},"aggregations": {"user_type": {..."buckets": [{"key": 4,"doc_count": 1220, //去重前數據1220條"count": {"value": 276 //去重后數據276條}},{"key": 3,"doc_count": 488, //去重前數據488條"count": {"value": 121 //去重后數據121條}}]}} }

  

4 - count + distinct + group by

SELECT COUNT(DISTINCT(user_id)) FROM table WHERE user_id_type = 2 GROUP BY user_id;

  

總結:對于既有group by又有distinct的查詢要求,需要在aggs中嵌套子aggs

5 - 注意事項

collapse關鍵字

  • 折疊功能ES5.3版本之后才發布的。
  • 聚合&折疊只能針對keyword類型有效



  • 轉載于:https://www.cnblogs.com/taozi32/p/10411524.html

    總結

    以上是生活随笔為你收集整理的ElasticSearch中distinct,count和group by的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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