参考资料:

https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_combining_the_two.html

特定概念:

桶(Buckets)满足特定条件的文档的集合

指标(Metrics)对桶内的文档进行统计计算

SELECT COUNT(color)
FROM table
GROUP BY color

COUNT(color) 相当于指标。

GROUP BY color 相当于桶。

聚合例子:

curl -XGET 'localhost:9200/text/_search?pretty' -d '
{"size":0,
"aggs":
{"popular_ports":
{"terms":
{"field":"port"}
}
}
}'

对port字段做聚合,结果名称为popular_ports。size设为0表示不关心搜索结果,结果的hits部分会是空值。

计算每种颜色车辆价格的平均值

GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

嵌套桶

每个颜色的汽车制造商的分布

GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"make": {
"terms": {
"field": "make"
}
}
}
}
}
}

每个颜色的汽车制造商的分布,以及各个制造商生产汽车的最高和最低价格。

GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": { "avg": { "field": "price" }
},
"make" : {
"terms" : {
"field" : "make"
},
"aggs" : {
"min_price" : { "min": { "field": "price"} },
"max_price" : { "max": { "field": "price"} }
}
}
}
}
}
}

条形图

histogram

GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{
"field": "price",
"interval": 20000
},
"aggs":{
"revenue": {
"sum": {
"field" : "price"
}
}
}
}
}
}

terms桶  extended_stats度量

GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"makes": {
"terms": {
"field": "make",
"size": 10
},
"aggs": {
"stats": {
"extended_stats": {
"field": "price"
}
}
}
}
}
}

最新文章

  1. Sql Server随机取数据
  2. 最长递增子序列LIS再谈
  3. js数组的迭代
  4. Android获取ip地址
  5. Javascript:重用之道
  6. java程序使用memcached
  7. iOS 之 设置控件在视图中心位置
  8. 用一个jsp实现对数据库发访问
  9. 记录一次因为硬盘写满造成的redis无法连接
  10. JAVA进阶15
  11. Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible script 的格式不对,应改成Unix编码
  12. 毕业不到一年,绩效打了个D!
  13. JavaScript之函数(上)
  14. Python高级网络编程系列之终极篇---自己实现一个Web框架
  15. BZOJ 3503 [CQOI2014]和谐矩阵
  16. 金融量化分析【day113】:PGEC策略
  17. day 20 collection模块 time 模块 os 模块
  18. 代码直连指定ip的dubbo服务
  19. C#读取配置文件app.config
  20. Spring之事务操作(注解)

热门文章

  1. scrapy xpath、正则表达式、css选择器
  2. js变量浅谈
  3. python爬虫 beutifulsoup4_1官网介绍
  4. 虚拟化技术之KVM
  5. Hadoop生态圈-Oozie实战之调度shell脚本
  6. 解决win10无线无故断网,重启才恢复正常的情况【原】
  7. SpringJMS解析--监听器
  8. 7.SpringBoot 之 Web
  9. [C++]指针/指针数组/数组指针/多维指针/单值指针/多值指针
  10. luogu P1268 树的重量