Elasticsearch SpringBoot 整合 复杂检索
官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html
一、例子
package com.atguigu.gulimall.search; import com.alibaba.fastjson.JSON; import com.atguigu.gulimall.search.config.SearchConfig; import lombok.Data; import lombok.ToString; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilder; 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.aggregations.metrics.AvgAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class GulimallSearchApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; @Test public void contextLoads() { System.out.println(restHighLevelClient); } /** * * @throws IOException */ @Test public void searchData() throws IOException { //1、创建一个检索请求 SearchRequest searchRequest = new SearchRequest(); //从哪开始检索,检索条件是什么 searchRequest.indices("bank"); //指定DSL,检索条件 //SearchSourceBuilder source() //searchSourceBuilder 封装的条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //1.1 构造检索条件 // searchSourceBuilder.query(); // searchSourceBuilder.from(); // searchSourceBuilder.size(); // searchSourceBuilder.aggregation(); searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill")); //按照年龄的值分布进行聚合 TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10); searchSourceBuilder.aggregation(ageAgg); //1.3)、计算平均薪资 AvgAggregationBuilder builderAvg = AggregationBuilders.avg("balanceAvg").field("balance"); searchSourceBuilder.aggregation(builderAvg); System.out.println("检索条件"+searchSourceBuilder.toString()); searchRequest.source(searchSourceBuilder); //2、执行搜索 SearchResponse searchResponse = restHighLevelClient.search(searchRequest,SearchConfig.COMMON_OPTIONS); //3 分析结果 System.out.println(searchResponse.toString()); //3.1 获取所有查到的数据 SearchHits hits = searchResponse.getHits(); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit: searchHits) { String sourceAsString = searchHit.getSourceAsString(); Account account = JSON.parseObject(sourceAsString, Account.class); System.out.println("searchHit:"+account.toString()); } //3.2 获取这次检索到的分析信息 Aggregations aggregations = searchResponse.getAggregations(); // for (Aggregation aggregation : aggregations.asList()) { // aggregation.getName(); // System.out.println("当前聚合:"+aggregation.getName()); // } Terms ageAgg1 = aggregations.get("ageAgg"); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { String keyAsString = bucket.getKeyAsString(); System.out.println("年龄:"+keyAsString+"有几个人"+bucket.getDocCount()); } Avg balanceAvg = aggregations.get("balanceAvg"); System.out.println("平均薪资"+balanceAvg.getValue()); } @ToString @Data static class Account { private int account_number; private int balance; private String firstname; private String lastname; private int age; private String gender; private String address; private String employer; private String email; private String city; private String state; } }