Elasticsearch不仅仅适合做全文检索,分析聚合功能也很好用。下面通过实例来学习。

一、准备数据

{"index":{ "_index": "books", "_type": "IT", "_id": "1" }}
{"id":"1","title":"Java编程思想","language":"java","author":"Bruce Eckel","price":70.20,"year": 2007,"description":"Java学习必读经典,殿堂级著作!赢得了全球程序员的广泛赞誉。"} {"index":{ "_index": "books", "_type": "IT", "_id": "2" }}
{"id":"2","title":"Java程序性能优化","language":"java","author":"葛一鸣","price":46.50,"year": 2012,"description":"让你的Java程序更快、更稳定。深入剖析软件设计层面、代码层面、JVM虚拟机层面的优化方法"} {"index":{ "_index": "books", "_type": "IT", "_id": "3" }}
{"id":"3","title":"Python科学计算","language":"python","author":"张若愚","price":81.40,"year": 2016,"description":"零基础学python,光盘中作者独家整合开发winPython运行环境,涵盖了Python各个扩展库"} {"index":{ "_index": "books", "_type": "IT", "_id": "4" }}
{"id":"4","title":"Python基础教程","language":"python","author":"张若愚","price":54.50,"year": 2014,"description":"经典的Python入门教程,层次鲜明,结构严谨,内容翔实"} {"index":{ "_index": "books", "_type": "IT", "_id": "5" }}
{"id":"5","title":"JavaScript高级程序设计","language":"javascript","author":"Nicholas C.Zakas","price":66.40,"year":2012,"description":"JavaScript技术经典名著"}

准备5条数据,保存着books.json中,批量导入:

curl -XPOST "http://localhost:9200/_bulk?pretty" --data-binary @books.json

二、Group By分组统计

执行命令:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"per_count": {
"terms": {
"field": "language"
}
}
}
}'

统计结果:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"per_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "java",
"doc_count" : 2
}, {
"key" : "python",
"doc_count" : 2
}, {
"key" : "javascript",
"doc_count" : 1
} ]
}
}
}

按编程语言分类,java类2本,python类1本,javascript类1本。

三、Max最大值

执行命令,统计price最大的:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}'

返回结果:

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"max_price" : {
"value" : 81.4
}
}
}

四、Min最小值

求价格最便宜的那本:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}'

统计结果:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"max_price" : {
"value" : 81.4
}
}
}

五、Average平均值

分组统计并求5本书的平均价格:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"per_count": {
"terms": {
"field": "language"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
'

返回结果:

{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"per_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "java",
"doc_count" : 2,
"avg_price" : {
"value" : 58.35
}
}, {
"key" : "python",
"doc_count" : 2,
"avg_price" : {
"value" : 67.95
}
}, {
"key" : "javascript",
"doc_count" : 1,
"avg_price" : {
"value" : 66.4
}
} ]
}
}
}

六、Sum求和

求5本书总价:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '
{
"size": 0,
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}'

返回结果:

{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"sum_price" : {
"value" : 319.0
}
}
}

七、基本统计

基本统计会返回字段的最大值、最小值、平均值、求和:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"grades_stats": {
"stats": {
"field": "price"
}
}
}
}'

返回结果:

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"grades_stats" : {
"count" : 5,
"min" : 46.5,
"max" : 81.4,
"avg" : 63.8,
"sum" : 319.0
}
}
}

八、高级统计

高级统计还会返回方差、标准差等:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d'
{
"size": 0,
"aggs": {
"grades_stats": {
"extended_stats": {
"field": "price"
}
}
}
}
'

统计结果:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"grades_stats" : {
"count" : 5,
"min" : 46.5,
"max" : 81.4,
"avg" : 63.8,
"sum" : 319.0,
"sum_of_squares" : 21095.46,
"variance" : 148.65199999999967,
"std_deviation" : 12.19229264740638,
"std_deviation_bounds" : {
"upper" : 88.18458529481276,
"lower" : 39.41541470518724
}
}
}
}

九、百分比统计

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '
{
"size": 0,
"aggs": {
"load_time_outlier": {
"percentiles": {
"field": "year"
}
}
}
}
'

返回结果:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"load_time_outlier" : {
"values" : {
"1.0" : 2007.2,
"5.0" : 2008.0000000000002,
"25.0" : 2012.0,
"50.0" : 2012.0,
"75.0" : 2014.0,
"95.0" : 2015.6000000000001,
"99.0" : 2015.92
}
}
}
}

十、分段统计

统计价格小于50、50-80、大于80的百分比:

curl -XPOST "http://localhost:9200/books/_search?pretty" -d '{
"size": 0,
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [{
"to": 50
}, {
"from": 50,
"to": 80
}, {
"from": 80
}]
}
}
}
}
'

返回结果:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"price_ranges" : {
"buckets" : [ {
"key" : "*-50.0",
"to" : 50.0,
"to_as_string" : "50.0",
"doc_count" : 1
}, {
"key" : "50.0-80.0",
"from" : 50.0,
"from_as_string" : "50.0",
"to" : 80.0,
"to_as_string" : "80.0",
"doc_count" : 3
}, {
"key" : "80.0-*",
"from" : 80.0,
"from_as_string" : "80.0",
"doc_count" : 1
} ]
}
}
}

转自:http://blog.csdn.net/napoay/article/details/53484730

(转)Elasticsearch分析聚合的更多相关文章

  1. Elasticsearch分析聚合

    原文链接:http://blog.csdn.net/napoay/article/details/53484730

  2. Elasticsearch 之聚合分析入门

    本文主要介绍 Elasticsearch 的聚合功能,介绍什么是 Bucket 和 Metric 聚合,以及如何实现嵌套的聚合. 首先来看下聚合(Aggregation): 什么是 Aggregati ...

  3. Elasticsearch(8) --- 聚合查询(Metric聚合)

    Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...

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

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

  5. Elasticsearch系列---聚合查询原理

    概要 本篇主要介绍聚合查询的内部原理,正排索引是如何建立的和优化的,fielddata的使用,最后简单介绍了聚合分析时如何选用深度优先和广度优先. 正排索引 聚合查询的内部原理是什么,Elastich ...

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

    Elasticsearch(9) --- 聚合查询(Bucket聚合) 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch(8) --- 聚合查询(Metri ...

  7. [Elasticsearch] ES聚合场景下部分结果数据未返回问题分析

    背景 在对ES某个筛选字段聚合查询,类似groupBy操作后,发现该字段新增的数据,聚合结果没有展示出来,但是用户在全文检索新增的筛选数据后,又可以查询出来, 针对该问题进行了相关排查. 排查思路 首 ...

  8. ElasticSearch 的 聚合(Aggregations)

    Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...

  9. ES 24 - 如何通过Elasticsearch进行聚合检索 (分组统计)

    目录 1 普通聚合分析 1.1 直接聚合统计 1.2 先检索, 再聚合 1.3 扩展: fielddata和keyword的聚合比较 2 嵌套聚合 2.1 先分组, 再聚合统计 2.2 先分组, 再统 ...

随机推荐

  1. Shell 中的反引号(`),单引号('),双引号(")

    在写shell的时候老是傻傻分不清楚,今天来理一理. 1.反引号位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引号(')位于Enter键的左方的区别. 在Linux中起着命令替换的作用.命 ...

  2. Qt编写气体安全管理系统(界面超漂亮)

    自从把Qt样式表葵花宝典这个pdf文件看完以后,将所有的qss内容都轮了一遍,还写了个皮肤生成器工具,https://blog.csdn.net/feiyangqingyun/article/deta ...

  3. 【大数据系列】HDFS文件权限和安全模式、安装

    HDFS文件权限 1.与linux文件权限类型 r:read w:write x:execute权限x对于文件忽略,对于文件夹表示是否允许访问其内容 2.如果linux系统用户sanglp使用hado ...

  4. 开发常见错误之 :Missing artifact com.sun:tools:jar 1.7.0

    Missing artifact com.sun:tools:jar 1.7.0 解决办法一: 手动配置pom.xml,添加一个dependency如下: <dependency> < ...

  5. [转]OpenStack Neutron解析

    1.为什么还需要linux bridge的部署方式? 2.哪一个网桥起着交换机的作用? 3.neutron如何实现私有网络的隔离 =================================== ...

  6. Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1

    退出当前环境: logout (再重新登录进去) yum install python-devel  -yyum install libevent-devel  -y 把环境更新下yum instal ...

  7. 使用JSP的fmt标签实现国际化支持

    使用JSP的fmt标签配置i18n国际化资源文件可以实现根据不同的地区和语言切换不同的显示. 具体做法如下: 1.在JSP页面中添加fmt标签的引用: <%@ taglib prefix=&qu ...

  8. 利用开源架构ELK构建分布式日志系统

    问题导读 1.ELK产生的背景?2.ELK的基本组成模块以及各个模块的作用?3.ELK的使用总计有哪些? 背景 日志,对每个系统来说,都是很重要,又很容易被忽视的部分.日志里记录了程序执行的关键信息, ...

  9. python基础-动态加载lazy_import(利用__import__)

    看了一天动态加载,普遍有这么几种方法,总结一下,由简入深,本文仅对查到的栗子们做个引用……省去你们大把查资料的时间= = 主要思想:把模块(文件)名.类名.方法名当成了变量 然后利用__import_ ...

  10. 远程服务器git搭建

    在远程服务器如:/var/www下创建hello.git 然后git init --bare hello.git cd hello.git会看到下面的目录和文件 然后创建可以访问git的用户 git ...