update at 2021-03-07 17:31:22 by ehlxr
This commit is contained in:
commit
510e085195
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
60
pom.xml
Normal file
60
pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>io.github.ehlxr</groupId>
|
||||
<artifactId>springboot-es</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-es</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<!-- 修改 es 版本 -->
|
||||
<elasticsearch.version>7.10.2</elasticsearch.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package io.github.ehlxr.es;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringbootEsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootEsApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
7
src/main/resources/application.yml
Normal file
7
src/main/resources/application.yml
Normal file
@ -0,0 +1,7 @@
|
||||
server:
|
||||
port: 8899
|
||||
|
||||
spring:
|
||||
elasticsearch:
|
||||
rest:
|
||||
uris: http://127.0.0.1:9200
|
@ -0,0 +1,104 @@
|
||||
package io.github.ehlxr.es;
|
||||
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.Avg;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringbootEsApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private RestHighLevelClient restHighLevelClient;
|
||||
|
||||
@Test
|
||||
void searchData() throws IOException {
|
||||
/*
|
||||
* GET bank/_search
|
||||
* {
|
||||
* "query": {
|
||||
* "match_all": {}
|
||||
* },
|
||||
* "aggs": {
|
||||
* "ageAgg": {
|
||||
* "terms": {
|
||||
* "field": "age",
|
||||
* "size": 100
|
||||
* },
|
||||
* "aggs": {
|
||||
* "genderAgg": {
|
||||
* "terms": {
|
||||
* "field": "gender.keyword",
|
||||
* "size": 10
|
||||
* },
|
||||
* "aggs": {
|
||||
* "balanceAvg": {
|
||||
* "avg": {
|
||||
* "field": "balance"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* },
|
||||
* "balanceAvg":{
|
||||
* "avg": {
|
||||
* "field": "balance"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* },
|
||||
* "size": 0
|
||||
*
|
||||
* }
|
||||
*/
|
||||
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg");
|
||||
ageAgg.field("age").size(100);
|
||||
|
||||
ageAgg.subAggregation(AggregationBuilders.avg("balanceAvg").field("balance"));
|
||||
|
||||
TermsAggregationBuilder genderAgg = AggregationBuilders.terms("genderAgg").field("gender.keyword");
|
||||
genderAgg.subAggregation(AggregationBuilders.avg("ageBalanceAvg").field("balance"));
|
||||
|
||||
ageAgg.subAggregation(genderAgg);
|
||||
|
||||
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||
sourceBuilder.query(QueryBuilders.matchAllQuery());
|
||||
sourceBuilder.aggregation(ageAgg);
|
||||
sourceBuilder.size(0);
|
||||
|
||||
SearchRequest request = new SearchRequest("bank");
|
||||
request.source(sourceBuilder);
|
||||
|
||||
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
||||
// System.out.println(response);
|
||||
|
||||
Aggregations aggregations = response.getAggregations();
|
||||
|
||||
Terms ageAggTerms = aggregations.get("ageAgg");
|
||||
ageAggTerms.getBuckets().forEach(ageAggBucket -> {
|
||||
Avg balanceAvg = ageAggBucket.getAggregations().get("balanceAvg");
|
||||
|
||||
System.out.println("年龄为 " + ageAggBucket.getKeyAsString() + " 的人数 " + ageAggBucket.getDocCount() + " 平均工资为 " + balanceAvg.getValue());
|
||||
|
||||
Terms genderAggTerms = ageAggBucket.getAggregations().get("genderAgg");
|
||||
genderAggTerms.getBuckets().forEach(genderAggBucket -> {
|
||||
Avg ageBalanceAvg = genderAggBucket.getAggregations().get("ageBalanceAvg");
|
||||
System.out.println(" 其中性别为 " + genderAggBucket.getKeyAsString() + " 的人数为 " + genderAggBucket.getDocCount() + " 平均工资为 " + ageBalanceAvg.getValue());
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user