ElasticSearch group statistics query

ES group query

maven dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Build configuration class:

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 public key;
    }

    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() {<!-- -->
        CredentialsProvidercredentialsProvider = new BasicCredentialsProvider();
    //Access username and password are the username and password you set when you created the Alibaba Cloud Elasticsearch instance, and are also the login username and password for the Kibana console.
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esServiceConfig.getEsUserName(), esServiceConfig.getEsPassword()));
        // Create a rest client through the builder, and configure the HttpClientConfigCallback of the http client.
        // Click the created Elasticsearch instance ID, and obtain the public network address on the basic information page, which is the address of the ES cluster.
        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 instance is constructed by REST low-level client builder.
        return new RestHighLevelClient(builder);
    }
    @Bean
    public ElasticsearchRestTemplate restTemplate() {<!-- -->
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}

Statistics are based on two fields:

--- inject
 @Autowired
 private ElasticsearchRestTemplate restTemplate;
--- method
{<!-- -->
//build query condition
BoolQueryBuilder queryBuilder = QueryBuilders. boolQuery();
//The first parameter is the alias name The second parameter is the name of the field to be counted
TermsAggregationBuilder agg1 = AggregationBuilders.terms("countArea").field("qy_area").size(10000);
TermsAggregationBuilder agg2 = AggregationBuilders.terms("countXueli").field("xueli").size(10000);
// qy_area is the sum field name, num is the result alias
SumAggregationBuilder sb = AggregationBuilders. sum("qy_area"). field("num");
// Group statistics total by qy_area field
agg1. subAggregation(sb);
// Group and count the total by the xueli field
agg2. subAggregation(sb);
// merge
agg1. subAggregation(agg2);
NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(queryBuilder).addAggregation(agg1).build();
SearchHits<RegionalDirectionDoc> searchHits = restTemplate.search(build,RegionalDirectionDoc.class);
Aggregations aggregations1 = searchHits.getAggregations();
// Get the aggregation result
Terms terms = (Terms) aggregations1.asMap().get("countArea");
//record statistical results
List<Map<String, Object>> mapList = new ArrayList<>();
for (Terms. Bucket bucket : terms. getBuckets())
{<!-- -->
    Map<String, Object> map = new HashMap<>(16);
    // The value of the aggregation field column
    String keyAsString = bucket. getKeyAsString();
    // The number corresponding to the aggregation field
    long docCount = bucket. getDocCount();
    map.put("qy_area", keyAsString);
    map. put("number", docCount);
    //Analyze the education statistics under the city
    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);
        // The value of the aggregation field column
        String keyAsString2 = bucket2. getKeyAsString();
        // The number corresponding to the aggregation field
        long docCount2 = bucket2. getDocCount();
        map2.put("xueli", keyAsString2);
        map2. put("number", docCount2);
        mapList2. add(map2);
    }
    map.put("xueliList", mapList2);
    mapList. add(map);
}
}

Entity definition:

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 {<!-- -->
    /**
     * Uniquely identifies
     */
    @Id
    private String id;
    /**
     * Education
     */
    @Field(type = FieldType. Keyword)
    private String xueli;
    /**
     * District number
     */
    @Field(type = FieldType. Keyword)
    private String code;
    /**
     * Difficult student category
     */
    @Field(type = FieldType. Keyword)
    private String kunnansheng_id;
    /**
     * Normal student category
     */
    @Field(type = FieldType. Keyword)
    private String shifansheng_id;
    /**
     * Student category
     */
    @Field(type = FieldType. Keyword)
    private String xueshengliebie_id;
    /**
     * Academic system
     */
    @Field(type = FieldType. Keyword)
    private String xuezhi;
    /**
     * political status
     */
    @Field(type = FieldType. Keyword)
    private String political;
    /**
     * graduate
     */
    @Field(type = FieldType. Keyword)
    private String biye;
    /**
     * class
     */
    @Field(type = FieldType. Keyword)
    private String class_id;
    /**
     * major
     */
    @Field(type = FieldType. Keyword)
    private String professional_id;
    /**
     * Faculty
     */
    @Field(type = FieldType. Keyword)
    private String department_id;
    /**
     * Graduation Destination Status 11 school-level pass
     */
    @Field(type = FieldType. Keyword)
    private String byqx_status;
    /**
     * school id
     */
    @Field(type = FieldType. Keyword)
    private String school_id;
    /**
     * The area where the enterprise is located
     */
    @Field(type = FieldType. Keyword)
    private String qy_area;
    /**
     * Province
     */
    @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 bid;
    }

    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;
    }
}

Create index file:

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"
        }
      }
    }
}