ES的聚合操作
构建数据:
@Test
public void createIndex(){
/**
* 创建索引
* */
client.admin().indices().prepareCreate("player").get();
}
/**
* 创建映射
*/
@Test
public void testCreateIndexMapping_boost() throws Exception{
/**
* 格式:
"mappings": {
"player": {
"properties": {
"name": {"index": "not_analyzed","type": "string"},
"age": {"type": "integer"},
"salary": {"type": "integer"},
"team": {"index": "not_analyzed","type": "string"},
"position": {"index": "not_analyzed","type": "string"}
}
}
}
*/
//构建json的数据格式,创建映射
XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
.startObject()
.startObject("player")
.startObject("properties")
.startObject("name").field("type","string").field("index", "not_analyzed").endObject()
.startObject("age").field("type","integer").endObject()
.startObject("salary").field("type","integer").endObject()
.startObject("team").field("type","string").field("index", "not_analyzed").endObject()
.startObject("position").field("type","string").field("index", "not_analyzed").endObject()
.endObject()
.endObject()
.endObject();
PutMappingRequest request = Requests.putMappingRequest("player")
.type("player")
.source(mappingBuilder);
client.admin().indices().putMapping(request).get();
}
@Test
public void BulkInsertDocument() throws IOException {
BulkRequestBuilder bulkRequest = client.prepareBulk();
// either use client#prepare, or use Requests# to directly build index/delete requests
bulkRequest.add(client.prepareIndex("player", "player", "1")
.setSource(jsonBuilder()
.startObject()
.field("name", "郭德纲")
.field("age", 33)
.field("salary",3000)
.field("team" , "cav")
.field("position" , "sf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "2")
.setSource(jsonBuilder()
.startObject()
.field("name", "于谦")
.field("age", 25)
.field("salary",2000)
.field("team" , "cav")
.field("position" , "pg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "3")
.setSource(jsonBuilder()
.startObject()
.field("name", "岳云鹏")
.field("age", 29)
.field("salary",1000)
.field("team" , "war")
.field("position" , "pg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "4")
.setSource(jsonBuilder()
.startObject()
.field("name", "孙越")
.field("age", 26)
.field("salary",2000)
.field("team" , "war")
.field("position" , "sg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "5")
.setSource(jsonBuilder()
.startObject()
.field("name", "张云雷")
.field("age", 26)
.field("salary",2000)
.field("team" , "war")
.field("position" , "pf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "6")
.setSource(jsonBuilder()
.startObject()
.field("name", "爱迪生")
.field("age", 40)
.field("salary",1000)
.field("team" , "tim")
.field("position" , "pf")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "7")
.setSource(jsonBuilder()
.startObject()
.field("name", "牛顿")
.field("age", 21)
.field("salary",500)
.field("team" , "tim")
.field("position" , "c")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "4")
.setSource(jsonBuilder()
.startObject()
.field("name", "爱因斯坦")
.field("age", 21)
.field("salary",300)
.field("team" , "tim")
.field("position" , "sg")
.endObject()
)
);
bulkRequest.add(client.prepareIndex("player", "player", "8")
.setSource(jsonBuilder()
.startObject()
.field("name", "特斯拉")
.field("age", 20)
.field("salary",500)
.field("team" , "tim")
.field("position" , "sf")
.endObject()
)
);
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
// process failures by iterating through each bulk response item
}
}
1:分组求count
计算每个球队的球员数量:
select team, count(*) as player_count from player group by team;
@Test
public void groupAndCount() {
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
System.out.println(teamTerms);
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
System.out.println("每个球队 :" + bucket.getKey() + "有 【"+bucket.getDocCount()+"】 个人");
}
}
2:Group by 多个字段
计算每个球队每个位置的球员数 select team, position, count(*) as pos_count from player group by team,position;
/**
* group by 多个字段
* 计算每个球队每个位置的球员数
* select team, position, count(*) as pos_count from player group by team, position;
* */
@Test
public void groupMutilFields() {
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
TermsAggregationBuilder potition = AggregationBuilders.terms("position_count").field("position");
//注意父子关系
team.subAggregation(potition);
//3:将聚合条件放入查询条件中
builder.addAggregation(team).addAggregation(potition);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map<String, Aggregation> position_map = bucket.getAggregations().asMap();
StringTerms potitionTerms = (StringTerms)position_map.get("position_count");
//9:对子集合下面的内容迭代 队名--位置--球员
Iterator<StringTerms.Bucket> sub_iterator = potitionTerms.getBuckets().iterator();
while (sub_iterator.hasNext()){
StringTerms.Bucket sub_bucket = sub_iterator.next();
System.out.println("球队 :" + bucket.getKey() + "下面的 "+sub_bucket.getKey()+"的位置 有"+sub_bucket.getDocCount());
}
}
}
3:分组求最大
计算每个球队年龄最大/最小/总/平均的球员年龄
select team, max(age) as max_age from player group by team;
/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, max(age) as max_age from player group by team;
*
* */
@Test
public void groupMax(){
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3: 指定要查哪一个字段的最大值
MaxAggregationBuilder ageFiled = AggregationBuilders.max("max_age").field("age");
//: 找到聚合关系:父子关系
team.subAggregation(ageFiled);
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map age_map = bucket.getAggregations().asMap();
int age = (int)((InternalMax) age_map.get("max_age")).getValue();
System.out.println("球队 :" + bucket.getKey() + " 最大年龄: "+age);
}
}
4:分组求最小
计算每个球队年龄最大/最小/总/平均的球员年龄 select team, min(age) as max_age from player group by team;
/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, min(age) as max_age from player group by team;
*
* */
@Test
public void groupMin(){
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3: 指定要查哪一个字段的最大值
MinAggregationBuilder ageFiled = AggregationBuilders.min("min_age").field("age");
//: 找到聚合关系:父子关系
team.subAggregation(ageFiled);
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map age_map = bucket.getAggregations().asMap();
int age = (int)((InternalMin) age_map.get("max_age")).getValue();
System.out.println("球队 :" + bucket.getKey() + " 最小年龄: "+age);
}
}
5:分组求平均值
计算每个球队年龄最大/最小/总/平均的球员年龄 select team, min(age) as max_age from player group by team;
/**
* 分组求:最大值、最小值、平均值
* 计算每个球队年龄最大/最小/总/平均的球员年龄
select team, min(age) as max_age from player group by team;
*
* */
@Test
public void groupAvg(){
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3: 指定要查哪一个字段的最大值
AvgAggregationBuilder ageFiled = AggregationBuilders.avg("avg_age").field("age");
//: 找到聚合关系:父子关系
team.subAggregation(ageFiled);
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map age_map = bucket.getAggregations().asMap();
Double age = ((InternalAvg) age_map.get("avg_age")).getValue();
System.out.println("球队 :" + bucket.getKey() + " 平均年龄: "+age);
}
}
6:分组求和
计算每个球队球员的平均年龄,同时又要计算总年薪 select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;
/**
* 分组求:最大值、最小值、平均值
* 计算每个球队球员的平均年龄,同时又要计算总年薪
select team, avg(age)as avg_age, sum(salary) as total_salary from player group by team;
*
* */
@Test
public void groupsum(){
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");
//3: 指定要查哪一个字段
AvgAggregationBuilder ageFiled = AggregationBuilders.avg("avg_age").field("age");
SumAggregationBuilder salaryField= AggregationBuilders.sum("sum_salary").field("salary");
//: 找到聚合关系:父子关系
team.subAggregation(ageFiled).subAggregation(salaryField);
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map age_map = bucket.getAggregations().asMap();
Double age = ((InternalAvg) age_map.get("avg_age")).getValue();
double sum_salary = ((InternalSum) age_map.get("sum_salary")).getValue();
System.out.println("球队 :" + bucket.getKey() + " 平均年龄: "+age + " 球队总salary:" + sum_salary);
}
}
7:聚合排序
计算每个球队总年薪,并按照总年薪倒序排列 select team, sum(salary) as total_salary from player group by team order by total_salary desc;
/**
* 排序
计算每个球队总年薪,并按照总年薪倒序排列
select team, sum(salary) as total_salary from player group by team order by total_salary desc;
* */
@Test
public void groupOrder(){
/**
*
* TermsBuilder teamAgg= AggregationBuilders.terms("team").order(Order.aggregation("total_salary ", false);
SumBuilder salaryAgg= AggregationBuilders.avg("total_salary ").field("salary");
sbuilder.addAggregation(teamAgg.subAggregation(salaryAgg));
* */
//1:构建查询提交
SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");
//2:指定聚合条件
TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team")
.order(Terms.Order.aggregation("total_salary", false));//false是降序,true是升序
//3: 指定要查哪一个字段
SumAggregationBuilder salaryField= AggregationBuilders.sum("total_salary").field("salary");
//: 找到聚合关系:父子关系
team.subAggregation(salaryField);
//3:将聚合条件放入查询条件中
builder.addAggregation(team);
//4:执行action,返回searchResponse
SearchResponse searchResponse = builder.get();
//5:将查询返回的searchResponse转换成map
Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
//6:取出聚合的字段
StringTerms teamTerms = (StringTerms)aggregationMap.get("player_count");
//7:对聚合的字段进行迭代
Iterator<StringTerms.Bucket> iterator = teamTerms.getBuckets().iterator();
while (iterator.hasNext()){
StringTerms.Bucket bucket = iterator.next();
//8:获取所有子聚合
Map age_map = bucket.getAggregations().asMap();
double sum_salary = ((InternalSum) age_map.get("total_salary")).getValue();
System.out.println("球队 :" + bucket.getKey() + " 球队总salary:" + sum_salary);
}
}
ES的聚合操作的更多相关文章
- (转载)es进行聚合操作时提示Fielddata is disabled on text fields by default
原文地址:http://blog.csdn.net/u011403655/article/details/71107415 根据es官网的文档执行 GET /megacorp/employee/_se ...
- (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
根据es官网的文档执行 GET /megacorp/employee/_search { "aggs": { "all_interests": { " ...
- es进行聚合操作时提示Fielddata is disabled on text fields by default
在进行派粗前,先执行以下操作 { "properties": { "updatedate": { "type": "text&qu ...
- ElasticSearch 学习记录之ES几种常见的聚合操作
ES几种常见的聚合操作 普通聚合 POST /product/_search { "size": 0, "aggs": { "agg_city&quo ...
- ES Terms 聚合数据不确定性
Elasticsearch是一个分布式的搜索引擎,每个索引都可以有多个分片,用来将一份大索引的数据切分成多个小的物理索引,解决单个索引数据量过大导致的性能问题,另外每个shard还可以配置多个副本,来 ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作
之前已将spring boot原生方式介绍了,接下将结介绍的是Elasticsearch聚合操作.聚合操作一般来说是解决一下复杂的业务,比如mysql中的求和和分组,由于博主踩的坑比较多,所以博客可能 ...
- java es 骤合操作
ElasticSearch java API - 聚合查询 以球员信息为例,player索引的player type包含5个字段,姓名,年龄,薪水,球队,场上位置.index的mapping为: &q ...
- 36.分组聚合操作—bucket进行多层嵌套
主要知识点: 分组聚合操作-嵌套bucket. 本讲以前面电商实例,从颜色到品牌进行下钻分析,每种颜色的平均价格,以及找到每种颜色每个品牌的平均价格. 比如说,现在红色的电视有4台,同 ...
随机推荐
- IIS7下配置web.config隐藏index.php
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- MySQL-快速入门(3)运算符
1.常见的运算符:算术运算符.比较运算符.逻辑运算符.位运算符. 算术运算符:+.-.*./.%(求余). 比较运算符:>.<.=.>=.<=.!=.in.between an ...
- Sql server 执行计划详解
序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...
- HTTP报文结构和内容(转)
HTTP请求报文格式就如下图所示: 1. 请求报文 一个HTTP请求报文由请求行(request line).请求头部(header).空行和请求数据4个部分组成. 大致结构是这样的: <req ...
- 剑指offer-复杂链表的复制-链表-python
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- onehot编码解释
什么是One-Hot编码? One-Hot编码,又称为一位有效编码,主要是采用N位状态寄存器来对N个状态进行编码,每个状态都由他独立的寄存器位,并且在任意时候只有一位有效. One-Hot编码是分类变 ...
- 小程序中页面兼容h5标签的解析
有时候当小程序向后台拿数据是一篇html标签的文章时,把它放进小程序会发现很多标签就不兼容,如果要一个个改又很麻烦,有没有方法可以很快地兼容html标签呢? 有个工具可以做到:wxParse 下载了它 ...
- [七月挑选]写个定时任务,从github下载代码到阿里ECS服务器上
title: 写个定时任务,从github下载代码到阿里ECS服务器上 根据前几篇博客中能自己创建一个博客,并在github.io上访问到自己的博客,但是如果自己有服务器,那怎么能定时获取github ...
- grep 查找文件
--递归查找目录下含有该字符串的所有文件 grep -rn "data_chushou_pay_info" /home/hadoop/nisj/automationDemand/ ...
- 十大基本功之testbench
1. 激励的产生 对于testbench而言,端口应当和被测试的module一一对应.端口分为input,output和inout类型产生激励信号的时候,input对应的端口应当申明为reg, o ...