search(14)- elastic4s-统计范围:global, filter,post-filter bucket
聚合一般作用在query范围内。不带query的aggregation请求实际上是在match_all{}查询范围内进行统计的:
GET /cartxns/_search
{
"aggs": {
"all_colors": {
"terms": {"field" : "color.keyword"}
}
}
}
} GET /cartxns/_search
{
"query": {
"match_all": {}
},
"aggs": {
"all_colors": {
"terms": {"field" : "color.keyword"}
}
}
}
}
上面这两个请求结果相同:
"aggregations" : {
"all_colors" : {
"doc_count_error_upper_bound" : ,
"sum_other_doc_count" : ,
"buckets" : [
{
"key" : "red",
"doc_count" :
},
{
"key" : "blue",
"doc_count" :
},
{
"key" : "green",
"doc_count" :
}
]
}
}
虽然很多时候我们都希望在query作用域下进行统计,但也会碰到需要统计不含任何query条件的汇总数。比如在统计某个车款平价售价的同时又需要知道全部车款的平均售价。这里全部车款平价售价就是一种global bucket统计:
GET /cartxns/_search
{
"query" : {
"match" : {"make.keyword": "ford"}
}
, "aggs": {
"avg_ford": {
"avg": {
"field": "price"
}
},
"avg_all" : {
"global": {},
"aggs": {
"avg_price": {
"avg": {"field": "price"}
}
}
} } }
搜索结果和聚合结果如下:
"hits" : {
"total" : {
"value" : ,
"relation" : "eq"
},
"max_score" : 1.2809337,
"hits" : [
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "NGVXAnIBSDa1Wo5UqLc3",
"_score" : 1.2809337,
"_source" : {
"price" : ,
"color" : "green",
"make" : "ford",
"sold" : "2014-05-18"
}
},
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "OWVYAnIBSDa1Wo5UTrf8",
"_score" : 1.2809337,
"_source" : {
"price" : ,
"color" : "blue",
"make" : "ford",
"sold" : "2014-02-12"
}
}
]
},
"aggregations" : {
"avg_all" : {
"doc_count" : ,
"avg_price" : {
"value" : 26500.0
}
},
"avg_ford" : {
"value" : 27500.0
}
}
用elastic4s来表达:
val aggGlob = search("cartxns").query(
matchQuery("make.keyword","ford")
).aggregations(
avgAggregation("single_avg").field("price"),
globalAggregation("all_avg").subaggs(
avgAggregation("avg_price").field("price")
)
)
println(aggGlob.show)
val globResult = client.execute(aggGlob).await
if (globResult.isSuccess) {
val gavg = globResult.result.aggregations.global("all_avg").avg("avg_price")
val savg = globResult.result.aggregations.avg("single_avg")
println(s"${savg.value},${gavg.value}")
globResult.result.hits.hits.foreach(h => println(s"${h.sourceAsMap}"))
} else println(s"error: ${globResult.error.causedBy.getOrElse("unknown")}")
...
POST:/cartxns/_search?
StringEntity({"query":{"match":{"make.keyword":{"query":"ford"}}},"aggs":{"single_avg":{"avg":{"field":"price"}},"all_avg":{"global":{},"aggs":{"avg_price":{"avg":{"field":"price"}}}}}},Some(application/json))
27500.0,26500.0
Map(price -> , color -> green, make -> ford, sold -> --)
Map(price -> , color -> blue, make -> ford, sold -> --)
filter-bucket的作用是:在query结果内再进行筛选后统计。比如:查询所有honda车款交易,但只统计honda某个月销售:
GET /cartxns/_search
{
"query": {
"match": {
"make.keyword": "honda"
}
},
"aggs": {
"sales_this_month": {
"filter": {
"range" : {"sold" : { "from" : "2014-10-01", "to" : "2014-11-01" }}
},
"aggs": {
"month_total": {
"sum": {"field": "price"}
}
}
}
}
}
首先,查询结果应该不受影响。同时还得到查询结果车款某个月的销售额:
"hits" : {
"total" : {
"value" : ,
"relation" : "eq"
},
"max_score" : 0.9444616,
"hits" : [
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "MmVXAnIBSDa1Wo5UqLc3",
"_score" : 0.9444616,
"_source" : {
"price" : ,
"color" : "red",
"make" : "honda",
"sold" : "2014-10-28"
}
},
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "M2VXAnIBSDa1Wo5UqLc3",
"_score" : 0.9444616,
"_source" : {
"price" : ,
"color" : "red",
"make" : "honda",
"sold" : "2014-11-05"
}
},
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "N2VXAnIBSDa1Wo5UqLc3",
"_score" : 0.9444616,
"_source" : {
"price" : ,
"color" : "red",
"make" : "honda",
"sold" : "2014-11-05"
}
}
]
},
"aggregations" : {
"sales_this_month" : {
"doc_count" : ,
"month_total" : {
"value" : 10000.0
}
}
}
elastic4s示范如下:
val aggfilter = search("cartxns").query(
matchQuery("make.keyword","honda")
).aggregations(
filterAgg("sales_the_month",rangeQuery("sold").gte("2014-10-01").lte("2014-11-01"))
.subaggs(sumAggregation("monthly_sales").field("price"))
)
println(aggfilter.show)
val filterResult = client.execute(aggfilter).await
if (filterResult.isSuccess) {
val ms = filterResult.result.aggregations.filter("sales_the_month")
.sum("monthly_sales").value
println(s"${ms}")
filterResult.result.hits.hits.foreach(h => println(s"${h.sourceAsMap}"))
} else println(s"error: ${filterResult.error.causedBy.getOrElse("unknown")}")
...
POST:/cartxns/_search?
StringEntity({"query":{"match":{"make.keyword":{"query":"honda"}}},"aggs":{"sales_the_month":{"filter":{"range":{"sold":{"gte":"2014-10-01","lte":"2014-11-01"}}},"aggs":{"monthly_sales":{"sum":{"field":"price"}}}}}},Some(application/json))
10000.0
Map(price -> , color -> red, make -> honda, sold -> --)
Map(price -> , color -> red, make -> honda, sold -> --)
Map(price -> , color -> red, make -> honda, sold -> --)
最后一个是post-filter。post-filter同样是对query结果的筛选,但是在完成了整个query后对结果的筛选。也就是说如果query还涉及到聚合,那么聚合不受筛选影响:
GET /cartxns/_search
{
"query": {
"match": {
"make.keyword": "ford"
}
},
"post_filter": {
"match" : {
"color.keyword" : "blue"
}
}
,"aggs": {
"colors": {
"terms": {
"field": "color.keyword",
"size":
}
}
}
}
查询和聚合结果如下:
"hits" : {
"total" : {
"value" : ,
"relation" : "eq"
},
"max_score" : 1.2809337,
"hits" : [
{
"_index" : "cartxns",
"_type" : "_doc",
"_id" : "OWVYAnIBSDa1Wo5UTrf8",
"_score" : 1.2809337,
"_source" : {
"price" : ,
"color" : "blue",
"make" : "ford",
"sold" : "2014-02-12"
}
}
]
},
"aggregations" : {
"colors" : {
"doc_count_error_upper_bound" : ,
"sum_other_doc_count" : ,
"buckets" : [
{
"key" : "blue",
"doc_count" :
},
{
"key" : "green",
"doc_count" :
}
]
}
}
}
可以看到:查询结果显示了经过post-filter筛选的结果,但聚合并没有受到filter影响。
elastic4s示范代码:
val aggPost = search("cartxns").query(
matchQuery("make.keyword","ford")
).postFilter(matchQuery("color.keyword","blue"))
.aggregations(
termsAgg("colors","color.keyword")
)
println(aggPost.show)
val postResult = client.execute(aggPost).await
if (postResult.isSuccess) {
postResult.result.hits.hits.foreach(h => println(s"${h.sourceAsMap}"))
postResult.result.aggregations.terms("colors").buckets
.foreach(b => println(s"${b.key},${b.docCount}"))
} else println(s"error: ${postResult.error.causedBy.getOrElse("unknown")}")
...
POST:/cartxns/_search?
StringEntity({"query":{"match":{"make.keyword":{"query":"ford"}}},"post_filter":{"match":{"color.keyword":{"query":"blue"}}},"aggs":{"colors":{"terms":{"field":"color.keyword"}}}},Some(application/json))
Map(price -> , color -> blue, make -> ford, sold -> --)
blue,
green,
search(14)- elastic4s-统计范围:global, filter,post-filter bucket的更多相关文章
- 用c#开发微信 (14) 微统计 - 阅读分享统计系统 4 部署测试 (最终效果图)
微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...
- 大数据入门到精通3-SPARK RDD filter 以及 filter 函数
一.如何处理RDD的filter 1. 把第一行的行头去掉 scala> val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigat ...
- dubbo 提示No such extension Filter for filter/com.alibaba.dubbo.rpc.Filter
配置时 <dubbo:provider filter="DubboExceptionFilter"></dubbo:provider> DubboExcep ...
- (四) ffmpeg filter学习-filter命令学习
http://blog.csdn.net/joee33/article/details/51946712 http://blog.csdn.net/tkp2014/article/details/53 ...
- Filter和Listener的应用——分IP统计网站访问次数
一:分析 统计工作需要在所有资源执行前进行,所以需要放在filter中 这个拦截器仅仅进行统计工作,不进行拦截,所以请求必须继续传递下去 用Map<String,integer>来保存数据 ...
- django-admin 仿写stark组件action,filter筛选过滤,search查询
写在StandLi里面的方法都是通过ModelSubject这个类里面的stand_li方法,在它里面用StandLi这个类实例化出来一个对象,这个实例化出来的对象把数据传给前端HTML模板进行渲染, ...
- 机器学习理论基础学习14.2---线性动态系统-粒子滤波 particle filter
一.背景 与卡曼滤波不同的是,粒子滤波假设隐变量之间(隐变量与观测变量之间)是非线性的,并且不满足高斯分布,可以是任意的关系. 求解的还是和卡曼滤波一样,但由于分布不明确,所以需要用采样的方法求解. ...
- Servlet之Filter详解
参考文献:http://www.cnblogs.com/zlbx/p/4888312.html Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时 ...
- Servlet之Filter详细讲解
Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息进行 ...
- 过滤器(Filter)
day21 过滤器概述 1 什么是过滤器 过滤器JavaWeb三大组件之一,它与Servlet很相似!不它过滤器是用来拦截请求的,而不是处理请求的. 当用户请求某个Servlet时,会先执行部署在这个 ...
随机推荐
- Serlvet容器与Web应用
对启动顺序的错误认识 之前一直有个观点,应用运行在Servlet容器中,因为从Servlet容器与Web应用的使用方式来看,确实很有这种感觉. 我们每次都是启动Servlet容器,然后再启动我们的应用 ...
- git工具上传项目到码云
首先,你需要在本地安装git客户端,此处简单易懂,略过然后,在本地建好文件夹,以本人为例,我的路径为 E:\git_project,此时需要通过鼠标右键选择:git bush here 如图所示然后会 ...
- 算法笔记刷题2(codeup 1928)
又磕了一晚上,多点测试真的很烦 ,完全不知道错哪里,后来发现是我变量名命名不规范导致自己晕了填错了,其实思路还是对的 我觉得书上的做法也还行,但我不太喜欢用二维数组,所以拿以前写的算天数的程序改装了一 ...
- 设置共享内存大小 【windows】
hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAG ...
- Python 3之bytes新特性
转载: Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分. 文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示. Python 3不会以任意隐式的方 ...
- shell脚本(多线程批量创建用户)
shell脚本中的多线程 很多场景中会用到多线程,例如备份数据库,有100个库,正常备份效率极其低下.有了多线程原本可能需要10个小时备份,现在分10个线程同时去干,只要一个小时就解决了.今天就介绍下 ...
- 聊聊flink的BlobStoreService
序 本文主要研究一下flink的BlobStoreService BlobView flink-release-1.7.2/flink-runtime/src/main/java/org/apache ...
- JAVA I/O 与装饰者模式UML图
- 初识DP动态规划
一.多阶段决策过程的最优化问题 在现实生活中,有类活 动的过程,由于 它的特殊性,可将过程分成若干个互相阶段.在它的每一阶段都需要作出决策,从而使整个过程达到最好的活动效果.当阶段决策的选取不是任意确 ...
- Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)
Hanh lives in a shared apartment. There are nn people (including Hanh) living there, each has a priv ...