使用 https://github.com/taowen/es-monitor 可以用 SQL 进行 elasticsearch 的查询。有的时候分桶聚合之后会产生很多的桶,我们只对其中部分的桶关心。最简单的办法就是排序之后然后取前几位的结果。

ORDER BY _term

SQL

$ cat << EOF | ./es_query.py http://127.0.0.1:9200
SELECT ipo_year, COUNT(*) FROM symbol GROUP BY ipo_year ORDER BY ipo_year LIMIT 2
EOF
{"COUNT(*)": 4, "ipo_year": 1972}
{"COUNT(*)": 1, "ipo_year": 1973}

Elasticsearch

{
"aggs": {
"ipo_year": {
"terms": {
"field": "ipo_year",
"order": [
{
"_term": "asc"
}
],
"size": 2
},
"aggs": {}
}
},
"size": 0
}

因为 ipo_year 是 GROUP BY 的字段,所以按这个排序用_term指代。

{
"hits": {
"hits": [],
"total": 6714,
"max_score": 0.0
},
"_shards": {
"successful": 1,
"failed": 0,
"total": 1
},
"took": 3,
"aggregations": {
"ipo_year": {
"buckets": [
{
"key": 1972,
"doc_count": 4
},
{
"key": 1973,
"doc_count": 1
}
],
"sum_other_doc_count": 2893,
"doc_count_error_upper_bound": 0
}
},
"timed_out": false
}

ORDER BY _count

SQL

$ cat << EOF | ./es_query.py http://127.0.0.1:9200
SELECT ipo_year, COUNT(*) AS ipo_count FROM symbol GROUP BY ipo_year ORDER BY ipo_count LIMIT 2
EOF
{"ipo_count": 1, "ipo_year": 1973}
{"ipo_count": 2, "ipo_year": 1980}

Elasticsearch

{
"aggs": {
"ipo_year": {
"terms": {
"field": "ipo_year",
"order": [
{
"_count": "asc"
}
],
"size": 2
},
"aggs": {}
}
},
"size": 0
}
{
"hits": {
"hits": [],
"total": 6714,
"max_score": 0.0
},
"_shards": {
"successful": 1,
"failed": 0,
"total": 1
},
"took": 2,
"aggregations": {
"ipo_year": {
"buckets": [
{
"key": 1973,
"doc_count": 1
},
{
"key": 1980,
"doc_count": 2
}
],
"sum_other_doc_count": 2895,
"doc_count_error_upper_bound": -1
}
},
"timed_out": false
}

ORDER BY 指标

SQL

$ cat << EOF | ./es_query.py http://127.0.0.1:9200
SELECT ipo_year, MAX(market_cap) AS max_market_cap FROM symbol
GROUP BY ipo_year ORDER BY max_market_cap LIMIT 2
EOF
{"max_market_cap": 826830000.0, "ipo_year": 1982}
{"max_market_cap": 847180000.0, "ipo_year": 2016}

Elasticsearch

{
"aggs": {
"ipo_year": {
"terms": {
"field": "ipo_year",
"order": [
{
"max_market_cap": "asc"
}
],
"size": 2
},
"aggs": {
"max_market_cap": {
"max": {
"field": "market_cap"
}
}
}
}
},
"size": 0
}
{
"hits": {
"hits": [],
"total": 6714,
"max_score": 0.0
},
"_shards": {
"successful": 1,
"failed": 0,
"total": 1
},
"took": 20,
"aggregations": {
"ipo_year": {
"buckets": [
{
"max_market_cap": {
"value": 826830000.0
},
"key": 1982,
"doc_count": 4
},
{
"max_market_cap": {
"value": 847180000.0
},
"key": 2016,
"doc_count": 6
}
],
"sum_other_doc_count": 2888,
"doc_count_error_upper_bound": -1
}
},
"timed_out": false
}

HISTOGRAM 和 ORDER BY

除了 terms aggregation,其他 aggregation 也支持 order by 但是并不完善。比如 histogram aggregation 支持 sort 但是并不支持 size (也就是可以ORDER BY 但是不能 LIMIT)。官方有计划增加一个通用的支持 LIMIT 的方式,不过还没有实现:https://github.com/elastic/elasticsearch/issues/14928

SQL

$ cat << EOF | ./es_query.py http://127.0.0.1:9200
SELECT ipo_year_range, MAX(market_cap) AS max_market_cap FROM symbol
GROUP BY histogram(ipo_year, 10) AS ipo_year_range ORDER BY ipo_year_range
EOF
{"ipo_year_range": 1970, "max_market_cap": 18370000000.0}
{"ipo_year_range": 1980, "max_market_cap": 522690000000.0}
{"ipo_year_range": 1990, "max_market_cap": 230940000000.0}
{"ipo_year_range": 2000, "max_market_cap": 470490000000.0}
{"ipo_year_range": 2010, "max_market_cap": 287470000000.0}

Elasticsearch

{
"aggs": {
"ipo_year_range": {
"aggs": {
"max_market_cap": {
"max": {
"field": "market_cap"
}
}
},
"histogram": {
"field": "ipo_year",
"interval": 10,
"order": {
"_key": "asc"
}
}
}
},
"size": 0
}
{
"hits": {
"hits": [],
"total": 6714,
"max_score": 0.0
},
"_shards": {
"successful": 1,
"failed": 0,
"total": 1
},
"took": 2,
"aggregations": {
"ipo_year_range": {
"buckets": [
{
"max_market_cap": {
"value": 18370000000.0
},
"key": 1970,
"doc_count": 5
},
{
"max_market_cap": {
"value": 522690000000.0
},
"key": 1980,
"doc_count": 155
},
{
"max_market_cap": {
"value": 230940000000.0
},
"key": 1990,
"doc_count": 598
},
{
"max_market_cap": {
"value": 470490000000.0
},
"key": 2000,
"doc_count": 745
},
{
"max_market_cap": {
"value": 287470000000.0
},
"key": 2010,
"doc_count": 1395
}
]
}
},
"timed_out": false
}

把 Elasticsearch 当数据库使:聚合后排序的更多相关文章

  1. es聚合后排序

    注意: es版本至少6.1以上 语句: GET 76/sessions/_search { "size": 0, "query": { "bool&q ...

  2. ElasticSearch 2 (34) - 信息聚合系列之多值排序

    ElasticSearch 2 (34) - 信息聚合系列之多值排序 摘要 多值桶(terms.histogram 和 date_histogram)动态生成很多桶,Elasticsearch 是如何 ...

  3. ElasticSearch 2 (37) - 信息聚合系列之内存与延时

    ElasticSearch 2 (37) - 信息聚合系列之内存与延时 摘要 控制内存使用与延时 版本 elasticsearch版本: elasticsearch-2.x 内容 Fielddata ...

  4. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  5. ElasticSearch 2 (35) - 信息聚合系列之近似聚合

    ElasticSearch 2 (35) - 信息聚合系列之近似聚合 摘要 如果所有的数据都在一台机器上,那么生活会容易许多,CS201 课商教的经典算法就足够应付这些问题.但如果所有的数据都在一台机 ...

  6. ElasticSearch 2 (29) - 信息聚合系列之测试驱动

    ElasticSearch 2 (29) - 信息聚合系列之测试驱动 摘要 我们可以用以下几页定义不同的聚合和它们的语法,但学习聚合的最佳途径就是用实例来说明.一旦我们获得了聚合的思想,以及如何合理地 ...

  7. Python全栈 MongoDB 数据库(聚合、二进制、GridFS、pymongo模块)

    断网了2天  今天补上     聚合操作: 对文档的信息进行整理统计的操作 返回:统计后的文档集合 db.collection.aggregate() 功能:聚合函数,完成聚合操作 参数:聚合条件,配 ...

  8. 使用Multipath进行多链路聚合并对聚合后的设备固定命名

    使用Multipath进行多链路聚合并对聚合后的设备固定命名 1.启用Multipath: (1)启动multipathd服务 #service multipathd start 或者 #/etc/i ...

  9. ElasticSearch 2 (33) - 信息聚合系列之聚合过滤

    ElasticSearch 2 (33) - 信息聚合系列之聚合过滤 摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版 ...

随机推荐

  1. shell字符串分割截取和转换总结

    一:字符串的截取 假定有定义变量VAR=mm/aa/bb/dd 1.获取字符串长度:echo "${#VAR}",即输出11: 2.非贪婪模式删除左边的,保留右边的:echo &q ...

  2. Redis集群的主从切换研究

    目录 目录 1 1. 前言 1 2. slave发起选举 2 3. master响应选举 5 4. 选举示例 5 5. 哈希槽传播方式 6 6. 一次主从切换记录1 6 6.1. 相关参数 6 6.2 ...

  3. js常见input校验

    //校验输入价格等,保留2位小数 function clearNoNum(obj){ obj.onkeyup = function(event){ var e = event || window.ev ...

  4. Ng第三课:线性代数回顾(Linear Algebra Review)

    3.1  矩阵和向量 3.2  加法和标量乘法 3.3  矩阵向量乘法 3.4  矩阵乘法 3.5  矩阵乘法的性质 3.6  逆.转置 3.1  矩阵和向量 如图:这个是 4×2 矩阵,即 4 行  ...

  5. Is Docker Good for Your Database?

    https://dzone.com/articles/is-docker-good-for-your-database

  6. Reorder the Books -- hdu -- 5500

    http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...

  7. KVM学习(初步安装与使用)

    本机环境介绍 本次使用Vmware workstation 12 pro版本号为12.5.2 build-4638234.虚拟机操作系统版本如下 [root@node2 ~]# cat /etc/re ...

  8. noip第24课作业

    1.  马走日 [问题描述] 马在中国象棋以日子形规则移动.请编写一段程序给定n*m大小的棋盘,以及马的初始位置(x,y),要求不能重复经过棋盘上的同一个点,计算马可以有多少途径遍历棋盘上的所有点. ...

  9. Yeoman安装和使用详解

    Yeoman generator-react-webpack 一 什么是Yeoman Yeoman帮助我们创建项目,提供更好的工具来使我们的项目更多样化. Yeoman提供generator系统,一个 ...

  10. mysql_触发器

    mysql触发器 触发器:trigger,事先为某张表绑定好一段代码,当表中某些内容发生改变的时候(增删改),系统会自动触发代码,执行 触发器:事件类型,触发时间,触发对象 事件类型:增删改,三种类型 ...