elasticsearch-dsl聚合-1
2024-08-25 10:55:03
接续上篇,本篇介绍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() - 未完待续...
- 新建一张测试表
最新文章
- 调用微信退款接口或发红包接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
- hive内部表、外部表、分区表、视图
- 2.一起来学hibernate之配置文件1与持久化对象
- Android中解决图像解码导致的OOM问题
- IE下AjaxForm上传文件直接提示下载的兼容性Bug
- java中的static变量
- bzoj3667: Rabin-Miller算法
- Apache POI解析excel文件
- [转] Ant 编译 Android 项目为 Apk 实战, 常见问题解决
- 安装Devstack的DNS问题
- 初步掌握Yarn的架构及原理(转)
- IP Editor IP控件(对比一下封装IP控件)
- LeetCode Solutions : Swap Nodes in Pairs
- 转:如何让LoadRunner实现多个场景运行?
- 用Redis实现优先级队列
- Spring MVC处理(下周完善)
- kubernetes入门(04)kubernetes的核心概念(1)
- 观察者模式之ES6实现(一)
- CentOS7 配置静态 ip
- JAVA I/O(一)基本字节和字符IO流
热门文章
- C知识要点-个人总结
- [HZOI 2015]复仇的序幕曲
- js 去掉字符串前后空格5种方法
- JavaSE之Java基础(1)
- div多选控制
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->;Preferences->;Launching preferences 其他activity获取Preference中的值
- SpringBoot常用应用程序属性
- Java -GUI开发九九乘法表
- check_mk 之 Check Parameters
- jshint-eclipse: JavaScript Code Quality Plugin for Eclipse