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 ( ) ;
credentialsProvider. setCredentials ( AuthScope . ANY, new UsernamePasswordCredentials ( esServiceConfig. getEsUserName ( ) , esServiceConfig. getEsPassword ( ) ) ) ;
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) ;
}
} ) ;
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 ) ;
SumAggregationBuilder sb = AggregationBuilders . sum ( "qy_area" ) . field ( "num" ) ;
agg1. subAggregation ( sb) ;
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;
@Field ( type = FieldType. Keyword )
private String byqx_status;
@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"
}
}
}
}