ES 分组查询
maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
构建配置类:
package cn.com.newcapec.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "sl")
public class EsServiceConfig {
private String publickey;
private String esUserName;
private String esPassword;
private String esHost;
private String esPort;
private String esScheme;
public String getEsUserName() {
return esUserName;
}
public void setEsUserName(String esUserName) {
this.esUserName = esUserName;
}
public String getEsPassword() {
return esPassword;
}
public void setEsPassword(String esPassword) {
this.esPassword = esPassword;
}
public String getEsHost() {
return esHost;
}
public void setEsHost(String esHost) {
this.esHost = esHost;
}
public String getEsPort() {
return esPort;
}
public void setEsPort(String esPort) {
this.esPort = esPort;
}
public String getEsScheme() {
return esScheme;
}
public void setEsScheme(String esScheme) {
this.esScheme = esScheme;
}
public String getPublickey() {
return publickey;
}
public void setPublickey(String publickey) {
this.publickey = publickey;
}
}
package cn.com.newcapec.config;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Autowired
private EsServiceConfig esServiceConfig;
@Override
public RestHighLevelClient elasticsearchClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//访问用户名和密码为您创建阿里云Elasticsearch实例时设置的用户名和密码,也是Kibana控制台的登录用户名和密码。
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esServiceConfig.getEsUserName(), esServiceConfig.getEsPassword()));
// 通过builder创建rest client,配置http client的HttpClientConfigCallback。
// 单击所创建的Elasticsearch实例ID,在基本信息页面获取公网地址,即为ES集群地址。
RestClientBuilder builder = RestClient.builder(new HttpHost(esServiceConfig.getEsHost(), Integer.valueOf(esServiceConfig.getEsPort()), esServiceConfig.getEsScheme())).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
// RestHighLevelClient实例通过REST low-level client builder进行构造。
return new RestHighLevelClient(builder);
}
@Bean
public ElasticsearchRestTemplate restTemplate() {
return new ElasticsearchRestTemplate(elasticsearchClient());
}
}
根据两个字段进行统计:
--- 注入
@Autowired
private ElasticsearchRestTemplate restTemplate;
--- 方法
{
//构建查询条件
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
//第一个参数是别名 第二个参数要统计的字段名称
TermsAggregationBuilder agg1 = AggregationBuilders.terms("countArea").field("qy_area").size(10000);
TermsAggregationBuilder agg2 = AggregationBuilders.terms("countXueli").field("xueli").size(10000);
// qy_area是求和字段名称,num是结果别名
SumAggregationBuilder sb = AggregationBuilders.sum("qy_area").field("num");
// 通过qy_area字段分组统计总数
agg1.subAggregation(sb);
// 通过xueli字段分组统计总数
agg2.subAggregation(sb);
// 合并
agg1.subAggregation(agg2);
NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(queryBuilder).addAggregation(agg1).build();
SearchHits<RegionalDirectionDoc> searchHits = restTemplate.search(build,RegionalDirectionDoc.class);
Aggregations aggregations1 = searchHits.getAggregations();
// 取出聚合结果
Terms terms = (Terms) aggregations1.asMap().get("countArea");
//记录统计结果
List<Map<String, Object>> mapList = new ArrayList<>();
for (Terms.Bucket bucket : terms.getBuckets())
{
Map<String, Object> map = new HashMap<>(16);
// 聚合字段列的值
String keyAsString = bucket.getKeyAsString();
// 聚合字段对应的数量
long docCount = bucket.getDocCount();
map.put("qy_area", keyAsString);
map.put("number", docCount);
//解析城市下学历统计
Aggregations aggregations2 = bucket.getAggregations();
Terms terms2 = (Terms) aggregations2.asMap().get("countXueli");
List<Map<String, Object>> mapList2 = new ArrayList<>();
for (Terms.Bucket bucket2 : terms2.getBuckets())
{
Map<String, Object> map2 = new HashMap<>(16);
// 聚合字段列的值
String keyAsString2 = bucket2.getKeyAsString();
// 聚合字段对应的数量
long docCount2 = bucket2.getDocCount();
map2.put("xueli", keyAsString2);
map2.put("number", docCount2);
mapList2.add(map2);
}
map.put("xueliList", mapList2);
mapList.add(map);
}
}
实体定义:
package cn.com.newcapec.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "regional_direction_entity")
public class RegionalDirectionDoc {
/**
* 唯一标识
*/
@Id
private String id;
/**
* 学历
*/
@Field(type = FieldType.Keyword)
private String xueli;
/**
* 区编号
*/
@Field(type = FieldType.Keyword)
private String code;
/**
* 困难生类别
*/
@Field(type = FieldType.Keyword)
private String kunnansheng_id;
/**
* 师范生类别
*/
@Field(type = FieldType.Keyword)
private String shifansheng_id;
/**
* 学生类别
*/
@Field(type = FieldType.Keyword)
private String xueshengliebie_id;
/**
* 学制
*/
@Field(type = FieldType.Keyword)
private String xuezhi;
/**
* 政治面貌
*/
@Field(type = FieldType.Keyword)
private String political;
/**
* 毕业
*/
@Field(type = FieldType.Keyword)
private String biye;
/**
* 班级
*/
@Field(type = FieldType.Keyword)
private String class_id;
/**
* 专业
*/
@Field(type = FieldType.Keyword)
private String professional_id;
/**
* 院系
*/
@Field(type = FieldType.Keyword)
private String department_id;
/**
* 毕业去向状态 11校本级通过
*/
@Field(type = FieldType.Keyword)
private String byqx_status;
/**
* 学校id
*/
@Field(type = FieldType.Keyword)
private String school_id;
/**
* 企业所在区
*/
@Field(type = FieldType.Keyword)
private String qy_area;
/**
* 所在省
*/
@Field(type = FieldType.Keyword)
private String shengyuan;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getXueli() {
return xueli;
}
public void setXueli(String xueli) {
this.xueli = xueli;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getKunnansheng_id() {
return kunnansheng_id;
}
public void setKunnansheng_id(String kunnansheng_id) {
this.kunnansheng_id = kunnansheng_id;
}
public String getShifansheng_id() {
return shifansheng_id;
}
public void setShifansheng_id(String shifansheng_id) {
this.shifansheng_id = shifansheng_id;
}
public String getXueshengliebie_id() {
return xueshengliebie_id;
}
public void setXueshengliebie_id(String xueshengliebie_id) {
this.xueshengliebie_id = xueshengliebie_id;
}
public String getXuezhi() {
return xuezhi;
}
public void setXuezhi(String xuezhi) {
this.xuezhi = xuezhi;
}
public String getPolitical() {
return political;
}
public void setPolitical(String political) {
this.political = political;
}
public String getBiye() {
return biye;
}
public void setBiye(String biye) {
this.biye = biye;
}
public String getClass_id() {
return class_id;
}
public void setClass_id(String class_id) {
this.class_id = class_id;
}
public String getProfessional_id() {
return professional_id;
}
public void setProfessional_id(String professional_id) {
this.professional_id = professional_id;
}
public String getDepartment_id() {
return department_id;
}
public void setDepartment_id(String department_id) {
this.department_id = department_id;
}
public String getByqx_status() {
return byqx_status;
}
public void setByqx_status(String byqx_status) {
this.byqx_status = byqx_status;
}
public String getSchool_id() {
return school_id;
}
public void setSchool_id(String school_id) {
this.school_id = school_id;
}
public String getQy_area() {
return qy_area;
}
public void setQy_area(String qy_area) {
this.qy_area = qy_area;
}
public String getShengyuan() {
return shengyuan;
}
public void setShengyuan(String shengyuan) {
this.shengyuan = shengyuan;
}
}
创建索引文件:
PUT regional_direction_entity
{
"mappings": {
"properties": {
"biye": {
"type": "keyword"
},
"byqx_status": {
"type": "keyword"
},
"class_id": {
"type": "keyword"
},
"code": {
"type": "keyword"
},
"department_id": {
"type": "keyword"
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"kunnansheng_id": {
"type": "keyword"
},
"political": {
"type": "keyword"
},
"professional_id": {
"type": "keyword"
},
"qy_area": {
"type": "keyword"
},
"school_id": {
"type": "keyword"
},
"shengyuan": {
"type": "keyword"
},
"shifansheng_id": {
"type": "keyword"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"xueli": {
"type": "keyword"
},
"xueshengliebie_id": {
"type": "keyword"
},
"xuezhi": {
"type": "keyword"
}
}
}
}
文章来源地址https://www.toymoban.com/news/detail-787210.html
文章来源:https://www.toymoban.com/news/detail-787210.html
到了这里,关于ElasticSearch分组统计查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!