elasticsearch-dsl聚合-1
接续上篇,本篇介绍elasticsearch聚合查询,使用python库elasticsearch-dsl进行聚合查询操作。
7.3、聚合查询
高阶概念
- Buckets(桶/集合):满足特定条件的文档的集合
Metrics(指标):对桶内的文档进行统计计算(例如最小值,求和,最大值等)
- 新建一张测试表
PUT cars
{
"mappings": {
"transactions":{
"properties": {
"price":{
"type": "integer"
},
"color":{
"type": "text",
"fielddata": true
},
"make":{
"type": "text",
"fielddata": true
},
"sold":{
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}插入数据
POST /cars/transactions/_bulk
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {"_index": "cars", "_type": "transactions"}}
{ "price" : , "color" : "blue", "make" : "ford", "sold" : "2014-02-12" } - 查询哪个颜色的汽车销量最好(按颜色分类)
GET cars/transactions/_search
{
"size": ,
"aggs": {
"popular_colors": {
"terms": {
"field": "color"
}
}
}
}s = Search(index='cars')
a = A("terms", field="color")
s.aggs.bucket("popular_color", a)
response = s.execute()或者
s.aggs.bucket("popular_color", "terms", field="color") - 查询每种颜色车的平均价格
GET cars/transactions/_search
{
"size": ,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}s = Search(index='cars')
a1 = A("terms", field="color")
a2 = A("avg", field="price")
s.aggs.bucket("colors", a1).metric("avg_price", a2)
response = s.execute()或者
s = Search(index='cars')
s.aggs.bucket("colors", "terms", field="color").metric("avg_price", "avg", field="price")
response = s.execute() - 先按颜色分,再按品牌分,再求每种品牌的均价
GET cars/transactions/_search
{
"size": ,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"make": {
"terms": {
"field": "make"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}s = Search(index='cars')
s.aggs.bucket("colors", "terms", field="color")
s.aggs["colors"].bucket("make", "terms", field="make")
s.aggs["colors"].aggs["make"].metric("avg_price", "avg", field="price")
response = s.execute() - 先按颜色分,再按品牌分,再求每种品牌的最高和最低价
GET cars/transactions/_search
{
"size": ,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"make": {
"terms": {
"field": "make"
},
"aggs": {
"min_price": {
"min": {
"field": "price"
}
},
"max_price": {
"max": {
"field": "price"
}
}
}
}
}
}
}
}s = Search(index='cars')
s.aggs.bucket("colors", "terms", field="color")
s.aggs["colors"].bucket("make", "terms", field="make")
s.aggs["colors"].aggs["make"].metric("min_price", "min", field="price")
s.aggs["colors"].aggs["make"].metric("max_price", "max", field="price")
response = s.execute() - 未完待续...
- 新建一张测试表
elasticsearch-dsl聚合-1的更多相关文章
- ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合
Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...
- Elasticsearch(8) --- 聚合查询(Metric聚合)
Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...
- Elasticsearch(9) --- 聚合查询(Bucket聚合)
Elasticsearch(9) --- 聚合查询(Bucket聚合) 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch(8) --- 聚合查询(Metri ...
- Elasticsearch 之聚合分析入门
本文主要介绍 Elasticsearch 的聚合功能,介绍什么是 Bucket 和 Metric 聚合,以及如何实现嵌套的聚合. 首先来看下聚合(Aggregation): 什么是 Aggregati ...
- Elasticsearch系列---聚合查询原理
概要 本篇主要介绍聚合查询的内部原理,正排索引是如何建立的和优化的,fielddata的使用,最后简单介绍了聚合分析时如何选用深度优先和广度优先. 正排索引 聚合查询的内部原理是什么,Elastich ...
- Elasticsearch DSL中Query与Filter的不同
Elasticsearch支持很多查询方式,其中一种就是DSL,它是把请求写在JSON里面,然后进行相关的查询. 举个DSL例子 GET _search { "query": { ...
- ElasticSearch 的 聚合(Aggregations)
Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...
- ElasticSearch - 信息聚合系列之聚合过滤
摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版本 elasticsearch版本: elasticsearch-2. ...
- [elk]elasticsearch dsl语句
例子1 统计1,有唱歌兴趣的 2,按年龄分组 3,求每组平均年龄 4,按平均年龄降序排序 sql转为dsl例子 # 每种型号车的颜色数 > 1的 SELECT model,COUNT(DISTI ...
- elasticsearch DSL查询
总结一个DSL的用法吧,语法网上查去,只记录一点心得,也是研究了半天,太麻烦了 先附上python代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ fro ...
随机推荐
- C++有关拷贝构造函数(默认/浅/深拷贝构造函数)
拷贝结构函数顾名思义就是复制对象. 先讲一下默认拷贝函数: 默认拷贝就是直接赋值,让程序调用默认拷贝结构函数. Student p1; Student p2 = p1//或者Student p2(p1 ...
- Hash表的原理
哈希的概念:Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩 ...
- 未能解析引用的程序集......因为它对不在当前目标框架“.NETFramework,Version=v4.0,Profile=Client”中的
解决方法:资源管理器下点击项目名(右键)属性--将.NET Framework 4 Client Profile改成.NET Framework 4 . 传送门:http://bbs.csdn.net ...
- html:option
注意事项: (1):In HTML the <option> tag has no end tag.HTML中<option>标签不需要关闭.In XHTML the < ...
- 2019.3.26判断是否回文(java实现)
我所有的文章都是对我总结学习的总结,那里不好或者冒犯了那里,我先对您说声对不起,请告知我进行改正. 今天java老师作业题目是判断是一个字符串否是回文: emmmm,我的思路是将字符串逆序,然后使用方 ...
- Maven常用插件整理
maven内置变量 ${basedir}表示项目根目录,即包含pom.xml文件的目录; ${version}表示项目版本; ${project.basedir}同${basedir}; ${proj ...
- 浏览器缓存介绍之sessionStorage、localStorage、Cookie
Cookie Cookie 是小甜饼的意思.顾名思义,cookie 确实非常小,它的大小限制为4KB左右,是网景公司的前雇员 Lou Montulli 在1993年3月的发明.它的主要用途有保存登录信 ...
- agc027D - Modulo Matrix(构造 黑白染色)
题意 题目链接 构造一个\(n * n\)的矩阵,要求任意相邻的两个数\(a,b\),使得\(max(a,b) \% min(a,b) \not = 0\) Sol 我的思路: 假设\(mod = 1 ...
- git 推送代码到远程端
git init git add . git commit -m "first commit" git remote add origin "地址" git p ...
- echarts渲染一个风向图
今天给大家说一个用echarts渲染一个风向图,这里图上其实有三个要素,风向,风级和能见度,主要还是讲讲代码里面的坑 1.风向图标方向修改以及设置 var ownData = echarts.util ...