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 ...
随机推荐
- [原创]Dubbo配置(Spring4+Hiberante4+Druid)
如果dubbo使用注解,并且spring也使用注解,如使用事务,则dubbo加过注解的类无法发布. <?xml version="1.0" encoding="UT ...
- eureka 源码
本文为eureka学习笔记,错误之处请指正. ----------------------------------------------------------- 1.服务生产者是怎么注册到配置中心 ...
- ubuntu下安装录屏软件
sudo add-apt-repository ppa:maarten-baert/simplescreenrecorder sudo apt-get update sudo apt-get inst ...
- Windows 2008 R2 防火墙允许Serv-U通过的方法
在Windows 2008 R2上安装了Serv-U FTP服务端软件之后,无法通过客户端连接,究其原因是Windows 2008的防火墙没有开启FTP端口,而且在防火墙上添加Serv-U程序也不行, ...
- 35、XPath的使用示例
使用Xpath获取页面元素 [参见W3C官网说明] http://www.w3school.com.cn/xpath/xpath_syntax.asp 以下Xpath路径都是获取下面地址的元素 ...
- pat1048. Find Coins (25)
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- Matlab 2013a 和 VS2010 混合编程
最近由于项目需求,某项目的算法是基于MATLAB完成的,在短时间内需要去调用算法功能.因此,基于MATLAB生成DLL, C 调用的方式完成. 环境:MATLAB 2013a + VS2010 + w ...
- 【安全测试】sql注入
SQL注入攻击是黑客对 数据库 进行攻击的常用手段之一,随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员越来越多,但是由于程序员水平及经验页参差不齐,相当大部分程序员在编写代码的时候没有 ...
- List转为DataTable并可以导出为Excel
using com.jd120.Core.Utility; using System; using System.Collections.Generic; using System.Data; usi ...
- [JAVA][Liferay] Configure sharding in multiple sites
create databases first portal-ext.properties配置 hibernate.dialect=org.hibernate.dialect.PostgreSQLDia ...