赞
踩
聚合是ES
除搜索功能外提供的针对ES
数据做统计分析的功能,聚合有助于根据搜索查询提供聚合数据,聚合查询是数据库中重要额功能特性,ES
作为搜索引擎兼数据库,同样提供了强大的聚合分析功能力,它是基于查询条件来对数据进行分桶、计算的方法,这种很类似与SQL
中的group by
再加上一些函数方法的操作。
在了解聚合查询之前需要注意的一点是:text
类型是不支持聚合的,主要是因为text
类型本身是分词的,通俗的说,如果一句话分成了多个词然后进行group by
操作,那么问题就出现了,到底对哪一个词进行group by
操作呢?无法指定!
PUT /fruit
{
"mappings":{
"properties":{
"title":"keyword"
},
"price":{
"type":"double"
},
"description":{
"type":"text"
}
}
}
PUT /fruit/_bulk {"index":{}} {"title":"面包","price":19.6,"description":"小面包很便宜"} {"index":{}} {"title":"旺旺牛奶","price":29.6,"description":"旺旺牛奶很好喝"} {"index":{}} {"title":"日本豆","price":9.0,"description":"日本豆很便宜"} {"index":{}} {"title":"大辣条","price":10.6,"description":"大辣条超级好吃"} {"index":{}} {"title":"海苔","price":49.6,"description":"海苔很一般"} {"index":{}} {"title":"小饼干","price":9.6,"description":"小饼干很小"} {"index":{}} {"title":"小葡萄","price":59.6,"description":"小葡萄很好吃"} {"index":{}} {"title":"小饼干","price":19.6,"description":"小饼干很小"} {"index":{}} {"title":"小饼干","price":59.6,"description":"小饼干很小"} {"index":{}} {"title":"小饼干","price":29.6,"description":"小饼干很小"} {"index":{}} {"title":"小饼干","price":39.6,"description":"小饼干很小"}
GET /fruit/_search { "query": { "match\_all": { } }, "aggs": { "price\_group": { "terms": { "field": "price" } } } }
GET /fruit/_search
{
"query": {
"match\_all": {}
},
"aggs": {
"max\_price": {
"max": {
"field": "price"
}
}
}
}
GET /fruit/_search { "query": { "match\_all": {} }, "size": 0, "aggs": { "min\_price": { "min": { "field": "price" } } } }
GET /fruit/_search
{
"query": {
"match\_all": {}
},
"size": 0,
"aggs": {
"min\_price": {
"sum": {
"field": "price"
}
}
}
}
GET /fruit/_search
{
"query": {
"match\_all": {}
},
"size": 0,
"aggs": {
"avg\_price": {
"avg": {
"field": "price"
}
}
}
}
在使用Java API
实现上述操作之前,有必要先了解一下实现过程中使用到的某些方法以及工具
常见的聚合查询:
ValueCountBuilder vcb= AggregationBuilders.count(“分组的名称”).field(“字段”);
CardinalityBuilder cb= AggregationBuilders.cardinality(“分组的名称”).field(“字段”);
FilterAggregationBuilder fab= AggregationBuilders.filter(“分组的名称”).filter(QueryBuilders.queryStringQuery(“字段:过滤值”));
TermsBuilder tb= AggregationBuilders.terms(“分组的名称”).field(“字段”);
SumBuilder sumBuilder= AggregationBuilders.max(“分组的名称”).field(“字段”);
AvgBuilder ab= AggregationBuilders.min(“分组的名称”).field(“字段”);
MaxBuilder mb= AggregationBuilders.avg(“分组的名称”).field(“字段”);
DateHistogramBuilder dhb= AggregationBuilders.dateHistogram(“分组的名称”).field(“字段”);
TopHitsBuilder thb= AggregationBuilders.topHits(“分组的名称”);
NestedBuilder nb= AggregationBuilders.nested(“分组的名称”).path(“字段”);
AggregationBuilders.reverseNested(“分组的名称”).path("字段 ");
使用Java API
实现上述在Kibana
中的各项操作
public class RestHighLevelClientForAggs { public static void main(String[] args) { RestHighLevelClient esClient = Client.getClient(); //基于terms 类型聚合 基于字段进行分组聚合 SearchRequest request = new SearchRequest("fruit"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder .query(QueryBuilders.matchAllQuery())//查询条件 //用来设置聚合处理 .aggregation(AggregationBuilders.terms("price\_group").field("price")) .size(0); request.source(sourceBuilder); SearchResponse response = null; try { response = esClient.search(request, RequestOptions.DEFAULT); //处理聚合的结果 Aggregations aggregations = response.getAggregations(); ParsedDoubleTerms doubleTerms = aggregations.get("price\_group"); List<? extends Terms.Bucket> buckets = doubleTerms.getBuckets(); for (Terms.Bucket bucket : buckets) { System.out.println(bucket.getKey()+" "+bucket.getDocCount()); } }catch (Exception e){ e.printStackTrace(); } } }
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
nimg.cn/6b6870eb043d4ee6b8ce2586e5a02c9e.png)
[外链图片转存中…(img-lXiLSZJr-1714720118852)]
[外链图片转存中…(img-IaZQxVHp-1714720118852)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。