ES学习笔记八-聚合搜索
ES中的聚合搜索可以理解为关系型数据库中的group by,将具有相同条件的数据分组,并分析每一组数据的不同表现。
high-level concepts
Buckets
Metrics
GET /cars/transactions/_search?search_type=count{ "aggs" : { 这是一个聚合查询 "colors" : { 此聚合查询的名字(自己定义) "terms" : { "field" : "color" 定义聚合条件。以color分组 } } }}
You’ll notice that we used the count
search_type. Because we don’t care about search results—the aggregation totals—the count
search_type will be faster because it omits the fetch phase.
在讲query 执行时,elasticsearch会分为两个阶段,query阶段,fetch阶段。我们并不需要查询结果,只需要知道统计结果,所以省去了fetch阶段,search_type=count使聚合查询更高效
{... "hits": { "hits": [] 没有数据是因为我们search_type=count 并没有fetch阶段 }, "aggregations": { "colors": { 你定义的聚合查询的名字 "buckets": [ { "key": "red", 红色分组 "doc_count": 4 符合此条件的文档数 }, { "key": "blue", "doc_count": 2 }, { "key": "green", "doc_count": 2 } ] } }}
adding a metric to the mix
GET /cars/transactions/_search?search_type=count{ "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { 最外层是aggs,用来包裹住我们的统计条件 "avg_price": { 统计名称 "avg": { "field": "price" 我们将计算每组的price平均值 } } } } }}
buckets inside buckets
分组数据的嵌套,group by color,make 先按 color分组,再按make分组
GET /cars/transactions/_search?search_type=count{ "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { 注意它的顺序。他统计的平均值,是紧接的上一个条件的统计值 "avg": { "field": "price" } }, "make": { "terms": { "field": "make" } } } } }}
one final modification
GET /cars/transactions/_search?search_type=count{ "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "make" : { "terms" : { "field" : "make" }, "aggs" : { 添加第二个聚合统计 统计的是以color和make分组后的数据 "min_price" : { "min": { "field": "price"} }, 最低价格 "max_price" : { "max": { "field": "price"} } 最高价格 } } } } }}
building bar charts 创建柱形图
{ "aggs":{ "price":{ "histogram":{ "field": "price", "interval": 20000 间隔2000 所得出来的结果是[0-19999,20000-399999,40000-59999,60000-79999] }, "aggs":{ "revenue": { "sum": { "field" : "price" } } } } }}
If search is the most popular activity in Elasticsearch, building date histograms must be the second most popular.Why would you want to use a date histogram?
GET /cars/transactions/_search?search_type=count{ "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd" } } }}
returning empty buckets
Yep, that’s right. We are missing a few months! By default, the date_histogram
(and histogram
too) returns only buckets that have a nonzero document count.
某些月份缺失了,因为没有数据,但更多的时候我们需要显示,即使没有数据。
GET /cars/transactions/_search?search_type=count{ "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd", "min_doc_count" : 0, 既然全部的月份都显示出来了为什么还要定义min_doc_count呢?原因:but by default Elasticsearch will return only buckets that are between the minimum and maximum value in your data.默认只返回最大值最小值啊 "extended_bounds" : { this parameter forces the entire year to be returned 全部的月份都要显示出来 "min" : "2014-01-01", "max" : "2014-12-31" } } } }}
extended example
GET /cars/transactions/_search?search_type=count{ "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "quarter", "format": "yyyy-MM-dd", "min_doc_count" : 0, "extended_bounds" : { "min" : "2014-01-01", "max" : "2014-12-31" } }, "aggs": { "per_make_sum": { "terms": { "field": "make" }, "aggs": { "sum_price": { "sum": { "field": "price" } } } }, "total_sum": { "sum": { "field": "price" } } } } }}
scoping aggregations
GET /cars/transactions/_search { "query" : { "match" : { "make" : "ford" } }, "aggs" : { "colors" : { "terms" : { "field" : "color" } } }}
query与aggs是同级别的
global bucket
GET /cars/transactions/_search?search_type=count{ "query" : { "match" : { "make" : "ford" } }, "aggs" : { "single_avg_price": { "avg" : { "field" : "price" } all doc match ford }, "all": { "global" : {}, global bucket has no parameters "aggs" : { "avg_price": { "avg" : { "field" : "price" } 这个操作针对所有的数据,而不是match ford的数据 } } } }}
filtered query
GET /cars/transactions/_search?search_type=count{ "query" : { "filtered": { "filter": { "range": { "price": { "gte": 10000 } } } } }, "aggs" : { "single_avg_price": { "avg" : { "field" : "price" } } }}
filter bucket
{ "query":{ "match": { "make": "ford" } }, "aggs":{ "recent_sales": { "filter": { 把filter用在aggs里。 "range": { "sold": { "from": "now-1M" } } }, "aggs": { "average_price":{ "avg": { "field": "price" 计算即符合match 又符合filter的price 平均值 } } } } }}
post filter
You may be thinking to yourself, "hmm…is there a way to filter just the search results but not the aggregation?" The answer is to use a post_filter
.
这个filter只对查询数据有效,对聚合操作无效,请使用post_filter
GET /cars/transactions/_search?search_type=count{ "query": { "match": { "make": "ford" } }, "post_filter": { "term" : { "color" : "green" } }, "aggs" : { "all_colors": { "terms" : { "field" : "color" } } }}
recap
重点回顾
在filtered中的filter 即会影响搜索结果,也会影响聚合结果
在aggs种的filter 只会影响聚合结果
在query中的post_filter只会影响搜索结果。
sorting multivalue buckets
对聚合结果进行排序,默认按照每个聚合结果中的doc_count降序排序。
intrinsic sorts
GET /cars/transactions/_search?search_type=count{ "aggs" : { "colors" : { "terms" : { "field" : "color", "order": { "_count" : "asc" 按照doc_count 升序排序 } } } }}
We introduce an order
object into the aggregation, which allows us to sort on one of several values:
-
_count
-
Sort by document count. Works with
terms
,histogram
,date_histogram
. -
_term
-
Sort by the string value of a term alphabetically. Works only with
terms
. -
_key
-
Sort by the numeric value of each bucket’s key (conceptually similar to
_term
). Works only withhistogram
anddate_histogram
.
sorting by a metric
GET /cars/transactions/_search?search_type=count{ "aggs" : { "colors" : { "terms" : { "field" : "color", "order": { "avg_price" : "asc" } }, "aggs": { "avg_price": { "avg": {"field": "price"} } } } }}
GET /cars/transactions/_search?search_type=count{ "aggs" : { "colors" : { "terms" : { "field" : "color", "order": { "stats.variance" : "asc" } }, "aggs": { "stats": { "extended_stats": {"field": "price"}This lets you override the sort order with any metric, simply by referencing the name of the metric. Some metrics, however, emit multiple values. The extended_stats
metric is a good example: it provides half a dozen individual metrics. } } } }}
sorting based on "deep" metrics
finding distinct counts
GET /cars/transactions/_search?search_type=count{ "aggs" : { "distinct_colors" : { "cardinality" : { "field" : "color" } } }}
ES学习笔记八-聚合搜索相关推荐
- Redis 学习笔记八:集群模式
Redis 学习笔记八:集群模式 作者:Grey 原文地址: 博客园:Redis 学习笔记八:集群模式 CSDN:Redis 学习笔记八:集群模式 前面提到的Redis 学习笔记七:主从复制和哨兵只能 ...
- OpenGL学习笔记(八):进一步理解VAO、VBO和SHADER,并使用VAO、VBO和SHADER绘制一个三角形
原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7888 ...
- ReactJS学习笔记八:动画
ReactJS学习笔记八:动画 分类: react学习笔记 javascript2015-07-06 20:27 321人阅读 评论(0) 收藏 举报 react动画 目录(?)[+] 这里只讨论Re ...
- 【opencv学习笔记八】创建TrackBar轨迹条
createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...
- python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件? 当我们点开下载页时, 一 ...
- ROS学习笔记八:创建ROS msg和srv
ROS学习笔记八:创建ROS msg和srv 本节主要讲述了如何创建和建立ROS msg和srv,同时使用命令行工具rosmsg.rossrv和roscp. msg和srv简介 msg:描述ROS m ...
- Halcon 学习笔记八:颜色识别
Halcon 学习笔记八:颜色识别 一.图像处理需要的知识 二.图像处理的预处理和分割过程 二.颜色识别的方法 三.例子一 四.例子二 五.例子三 一.图像处理需要的知识 1.图像处理基础(rgb(h ...
- ZooKeeper学习笔记(八):ZooKeeper集群写数据原理
写数据原理 写流程直接请求发送给Leader节点 这里假设集群中有三个zookeeper服务端 ACK (Acknowledge character)即是确认字符,在数据通信中,接收站发给发送站的一种 ...
- MongoDB 学习笔记八 复制、分片、备份与恢复、监控
MongoDB 学习笔记八 复制.分片.备份与恢复.监控 MongoDB复制(副本集) 什么是复制? MongoDB 复制原理 MongoDB 副本集设置 副本集添加成员 MongoDB 分片 分片 ...
- python3第三方库手册_python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑...
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件? 当我们点开下载页时, 一 ...
最新文章
- 中国碳酸氢钠干粉灭火剂市场产销分析与盈利前景策略报告2022年
- 刚刚,Python 3.10 正式发布了!我发现了一个可怕的功能...
- ISA2006标准版无人值守安装
- VIVOtech:使用近场通信(NFC)技术的免接触付款解决方案的市场领导者
- 用redis实现消息队列
- 【will】JS去字符串首尾空格
- Applet、Scriptlet与Servlet
- 资源下载的终极利器-资源轻松简单下载-资源万能下载法
- ubuntu linux ftp命令的使用
- 微信企业消息推送方案
- 教你如何批量修改图片分辨率?
- 计算机更改后怎么找不到桌面文件,电脑桌面的文件不见了怎么找回
- android10 imei横线,【报Bug】android10设备plus.device.getInfo获取imei为空
- 什么是球缺?球缺体积如何计算?计算公式?球缺应用:一半径为R的球沉入水中,球面顶部正好与水面相切,球的密度为1,求将球从水中取出所做的功?
- va_list(可变参数函数的使用)
- 免费高速的钉钉内网穿透——阿里出品必是精品(不限速,不限流量)
- win10c语言乱码修复方法,软件乱码 教你win10系统打开软件乱码的修复技巧
- 场效应晶体管在电路中的五大作用,你了解哪一种?
- 开发板Linux手指滑动方向,移动端 手指滑动方向获取
- java实现简单的搜索引擎
热门文章
- python 英语词汇_【我爱背单词】用Python提炼3000英语新闻高频词汇
- 音乐播放器mplayer的简单使用
- oracle datamodeler64,Toad Data Modeler6免费版
- 计算机中的数学【集合论】现代数学的共同基础
- 服务器安装系统提示加载驱动程序,解决安装win7的提示“加载驱动程序”的问题...
- 交易系统的高盈亏比怎么实现?
- [Mac] OSX 快捷键组合 (完整版)
- 个人作业4 结对开发地铁
- STM32F103_study49_The punctual atoms(STM32 Bit operation and logical operation in C language )
- 大白话5分钟带你走进人工智能-第十六节逻辑回归之分类的原因(1)