From 510e085195c22455615a7ed6c4dcb80cee5347a6 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sun, 7 Mar 2021 17:31:22 +0800 Subject: [PATCH] update at 2021-03-07 17:31:22 by ehlxr --- .gitignore | 33 ++++++ pom.xml | 60 ++++++++++ .../ehlxr/es/SpringbootEsApplication.java | 13 +++ src/main/resources/application.yml | 7 ++ .../es/SpringbootEsApplicationTests.java | 104 ++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/io/github/ehlxr/es/SpringbootEsApplication.java create mode 100644 src/main/resources/application.yml create mode 100644 src/test/java/io/github/ehlxr/es/SpringbootEsApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..09add20 --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.3 + + + io.github.ehlxr + springboot-es + 0.0.1-SNAPSHOT + springboot-es + Demo project for Spring Boot + + 1.8 + + 7.10.2 + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/io/github/ehlxr/es/SpringbootEsApplication.java b/src/main/java/io/github/ehlxr/es/SpringbootEsApplication.java new file mode 100644 index 0000000..a1ee3ed --- /dev/null +++ b/src/main/java/io/github/ehlxr/es/SpringbootEsApplication.java @@ -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); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..0a79fdf --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,7 @@ +server: + port: 8899 + +spring: + elasticsearch: + rest: + uris: http://127.0.0.1:9200 \ No newline at end of file diff --git a/src/test/java/io/github/ehlxr/es/SpringbootEsApplicationTests.java b/src/test/java/io/github/ehlxr/es/SpringbootEsApplicationTests.java new file mode 100644 index 0000000..a0bb1a8 --- /dev/null +++ b/src/test/java/io/github/ehlxr/es/SpringbootEsApplicationTests.java @@ -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()); + }); + + }); + } + +}