文章目录

  • 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类。

  1. metric aggregation: 主要是min,max,avg,sum,percetile 等单个统计指标的查询,同时,可以用作bucket agg的子聚合查询,但是本身不能包含子查询
  2. bucket aggregation: 主要是类似group by的查询操作,而且可以含有子查询
  3. matrix aggregation: 使用多个字段的值进行计算从而产生一个多维矩阵
  4. 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 查询相关推荐

  1. 02.elasticsearch bucket aggregation查询

    文章目录 1. bucket aggregation 查询类型概览 2. 数据准备 3. 使用样例 1. Terms Aggregation: 1. 普通的terms agg 2. 嵌套一个metri ...

  2. 03.elasticsearch pipeline aggregation查询

    文章目录 1. pipeline aggregation查询语法 1. 符号代表 2. 聚合层级 2. pipeline aggregation 查询类型概览 1. sibling aggregati ...

  3. Elasticsearch Metric Aggregation指标聚合详解

    文章目录 1. Avg 平均值 2. Max 最大值 3. Min 最小值 4. Sum 求和 5. Cardinality 基数(去重)聚合 6. Stats 统计聚合 7. Top Hits 聚合 ...

  4. 聚合中返回source_大数据搜索与可视化分析(9)elasticsearch聚合分析Metric Aggregation...

    在上一篇文章中,我们介绍了<大数据搜索与可视化分析(8)kibana入门教程-2-Discover>,本文学习elasticsearch聚合分析,是对<大数据搜索与可视化分析(3)e ...

  5. ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合

    Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...

  6. 举个栗子说明elasticsearch 的 scripted metric aggregation

    说明scripted metric aggregation 的用法. POST /dd/doc/_bulk?refresh {"index":{"_id":1} ...

  7. ElasticSearch+聚合+Aggregation+示例

    ElasticSearch+聚合+Aggregation+示例 聚合提供了分组并统计数据的能力.理解聚合的最简单的方式是将其粗略地等同为SQL的GROUP BY和SQL聚合函数.在Elasticsea ...

  8. Elasticsearch(9) --- 聚合查询(Bucket聚合)

    Elasticsearch(9) --- 聚合查询(Bucket聚合) 系统小说 www.kuwx.net 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch ...

  9. Elasticsearch:Aggregation 简介

    Aggregation 在中文中也被称作聚合.简单地说,Elasticsearch 中的 aggregation 聚合将你的数据汇总为指标.统计数据或其他分析.聚合可帮助你回答以下问题: 我的网站的平 ...

最新文章

  1. JGG:华中师大蒋兴鹏组-不同稀疏水平信号的微生物组关联检验方法
  2. SharePoint 集成OWA概述
  3. VIM自动格式化C代码
  4. java优先队列_Java高级特性增强-多线程
  5. 标准SPI、DUAL SPI、Quad SPI
  6. 博客改版日记9.7——内测先锋队总动员
  7. 【图像压缩】基于PCNN实现图像的压缩重建附matlab代码
  8. HTML5超链接链接ppt可以吗,PPT插入超链接的方法步骤详解
  9. 如何清除redis缓存
  10. Android Killer--安卓反编译工具
  11. STM32开发方式及基本介绍
  12. 拉格朗日松弛求解问题
  13. 雷爵代理游戏风云Unity休闲养成页游《宠物派对》
  14. laravel-admin引用wangEditor编辑器 使用二:上传视频/音频(2)
  15. more than and less than
  16. 迷你电脑将取代笨重的台式机
  17. spring源码项目使用spring-aspects
  18. SpringBoot定制化开发
  19. 如何使用渗透工具—nmap
  20. Macrorit Partition Expert v5.7.0 硬盘分区管理软件单文件中文版

热门文章

  1. 【Boost】boost库asio详解7——boost::asio::buffer用法
  2. STL中map用法详解
  3. live555 源码分析:播放启动
  4. Python中可变类型和不可变类型的数据?
  5. QUIC实战(四) 设置应用开机自启动
  6. Java类加载器(一)——类加载器层次与模型
  7. 高效终端设备视觉系统开发与优化
  8. 从上海到旧金山,2021 LiveVideoStackCon回归上海
  9. WebRTC十周年、Space X成功对接国际空间站、TikTok复制品Zynn或有快手支持|Decode the Week...
  10. Java多线程之CAS缺点