aggs

avg 平均数

最近15分钟的平均访问时间,upstream_time_ms是每次访问时间,单位毫秒

{
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
//当然你也可以直接将过滤器写在aggs里面
{
"size": 0,
"aggs": {
"filtered_aggs": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
},
"aggs": {
"execute_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}

cardinality 基数,比如计算uv

你可能注意到了size:0,如果你只需要统计数据,不要数据本身,就设置它,这不是我投机取巧,官方文档也是这么干的。

{
"size": 0,
"aggs": {
"filtered_aggs": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
},
"aggs": {
"ipv": {
"cardinality": {
"field": "ip"
}
}
}
}
}
}

percentiles 基于百分比统计

最近15分钟,99.9的请求的执行时间不超过多少

{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"percentiles": {
"field": "upstream_time_ms",
"percents": [
90,
95,
99.9
]
}
}
}
} //返回值,0.1%的请求超过了159ms
{
"took": 620,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 679400,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"values": {
"90.0": 24.727003484320534,
"95.0": 72.6200981699678,
"99.9": 159.01065773524886 //99.9的数据落在159以内,是系统计算出来159
}
}
}
}

percentile_ranks 指定一个范围,有多少数据落在这里

{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"percentile_ranks": {
"field": "upstream_time_ms",
"values": [
50,
160
]
}
}
}
} //返回值 {
"took": 666,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 681014,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"values": {
"50.0": 94.14716385885366,
"160.0": 99.91130872493076 //99.9的数据落在了160以内,这次,160是我指定的,系统计算出99.9
}
}
}
}

统计最近15分钟,不同的链接请求时间大小

{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"terms": {
"field": "uri"
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
} //返回,看起来url1 比 url2慢一点(avg_time),不过url1的请求量比较大 (doc_count)
{
"took": 1655,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 710802,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"doc_count_error_upper_bound": 10,
"sum_other_doc_count": 347175,
"buckets": [
{
"key": "/url1",
"doc_count": 362688,
"avg_time": {
"value": 6.601660380271749
}
},
{
"key": "/url2",
"doc_count": 939,
"avg_time": {
"value": 5.313099041533547
}
}
]
}
}
}

找出url响应最慢的前2名

{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-15m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"terms": {
"size": 2,
"field": "uri",
"order": {
"avg_time": "desc"
}
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}
//返回值
{
"took": 1622,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 748712,
"max_score": 0,
"hits": []
},
"aggregations": {
"execute_time": {
"doc_count_error_upper_bound": -1,
"sum_other_doc_count": 748710,
"buckets": [
{
"key": "url_shit",
"doc_count": 123,
"avg_time": {
"value": 8884
}
},
{
"key": "url_shit2",
"doc_count": 456,
"avg_time": {
"value": 8588
}
}
]
}
}
}

value_count 文档数量

相当于

select count(*) from table group by uri,为了达到这个目的,只需要把上文中,avg 换成value_count。不过avg的时候,结果中的doc_count其实达到了同样效果。

怎么取数据画个图?比如:最近2分钟,每20秒的时间窗口中,平均响应时间是多少

{
"size": 0,
"query": {
"filtered": {
"filter": {
"range": {
"@timestamp": {
"gt": "now-2m",
"lt": "now"
}
}
}
}
},
"aggs": {
"execute_time": {
"date_histogram": {
"field": "@timestamp",
"interval": "20s"
},
"aggs": {
"avg_time": {
"avg": {
"field": "upstream_time_ms"
}
}
}
}
}
}

pv 分时统计图(每小时一统计)

周期大小对性能影响不大

{
"size":0,
"fields":false,
"aggs": {
"execute_time": {
"date_histogram": {
"field": "@timestamp",
"interval": "1h"
}
}
}
}

Elasticsearch 统计代码例子的更多相关文章

  1. Linux下统计代码行数

    使用wc统计代码行数 最近写了一些代码,想统计一下代码的行数,在eclipse中好像没这功能,网上搜了一下才发现原来Linux有一个统计文件行数的命令wc.使用wc可以打印出每个文件和总文件的行数.字 ...

  2. VS2010统计代码行数 [转]

    按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容:  ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头 和 /开头 或者 空行 都不计入代 ...

  3. Eclipse统计代码行数

    开发过程中,经常需要统计代码行数,这时可以通过Eclipse的Search功能来实现. 步骤: 1.在Package Explorer中选中需要统计的包: 2.单击菜单Search-->File ...

  4. Google Analytics统计代码GA.JS中文教程

    2010-12-06 11:07:08|  分类: java编程 |  标签:google  analytics  ga  js  代码  |举报|字号 订阅     Google Analytics ...

  5. Visual Studio VS2010统计代码行数(转载)

    本文转自:http://blog.csdn.net/zhouworld16/article/details/9292851 在网上看到别人用的方法: 按CTRL+SHIFT+F (Find in fi ...

  6. 30个php操作redis常用方法代码例子

    From: http://www.jb51.net/article/51884.htm 这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型. ...

  7. 如何给WordPress安装百度统计代码

    1.注册并登录百度统计,点击页面顶部的“网站中心”,然后点击右上角“+ 新增网站”,填写网站域名确定后,点击“复制代码”:2.登录 WordPress 后台,点击左侧导航栏“外观”里的“编辑”,然后点 ...

  8. 在Flash Builder或者Eclipse统计代码行数的方法

    在Flash  Builder或者Eclipse统计代码行数的方法如下图菜单栏--搜索--搜索文件

  9. Python 统计代码行

    正在学习 Python, 做了个统计代码行的功能, 参考了网上很多前辈的帖子,添加了感觉还是比较实用的功能, 只是windows下测试了,而且代码文件编码形式是 utf-8的. 如果使用其它编码形式的 ...

随机推荐

  1. CentOS7 搭建RabbitMQ集群 后台管理 历史消费记录查看

    简介 通过 Erlang 的分布式特性(通过 magic cookie 认证节点)进行 RabbitMQ 集群,各 RabbitMQ 服务为对等节点,即每个节点都提供服务给客户端连接,进行消息发送与接 ...

  2. Java中的类与对象

    一.类与对象的概念 1.类:类是一组相同属性.方法的对象的集合:对象是类的具体化. 2.对象具有类所有的特征,类拥有的,对象就拥有. 3.类与对象他们的关系是相对的. 类有什么特点 1) 类是对象的类 ...

  3. 字体在mac win 系统如何优雅的展示

    我们知道,不同的操作系统,不同的浏览器,页面字体的显示和渲染存在差异. 那么如何设置font-family,能够使字体在不同的环境下,也拥有好的展示效果? 1.操作系统中字体默认的字体 windows ...

  4. redis 安装 配置 及启动

    linux下安装redis及其中遇到的问题的解决方法1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz# cd redis-3.0.2# mak ...

  5. throws、throw和try catch

    在学习代理模式的时候,编写动态生成代理类.java文件时,用try{}catch(){}捕获异常发现catch(Exception e)报错,得换成catch(Throwable e),然后又查了查两 ...

  6. Servlet基础知识总结

    Servlet是JavaWeb应用开发的核心组件.Servlet运行在Servlet容器中(例如最常用的Tomcat),它可以为各种客户请求提供相应服务.Servlet可以轻松完成以下任务: 动态生成 ...

  7. GoogleTest初探(2)

    前面的随笔介绍了Google Test中的基本测试单元TEST和测试夹具TEST_F,此篇介绍区别于这两种测试的参数化测试TEST_P 当待测试方法的行为取决于传入的参数时,而且这些参数的不同组合有多 ...

  8. IE浏览器中找不到开发者工具

    ie浏览器不知道什么原因开发者工具不见了.打开以后在任务栏中显示打开了控制台,但是看不到. 解决方法 : F12 打开开发者工具后,按下 “ Ctrl + P ”

  9. PhpStorm 全局查找的快捷键

    本页面查找 :   ctrl  + f 全局查找 : ctrl + shift + f 自己定义 :文件 -> 设置  -> 快捷键  ->  修改

  10. hive错误排查一:hive中执行 drop table命令卡住,删除表不成功

    起因 公司用的AWS EMR上的hive,突然不能删除表了. 经过 分析来看,估计是元数据那块出了问题.从元数据入手,元数据存在mysql的hive数据库中 直接使用hive配置文件hive-site ...