125、全文检索-ElasticSearch-整合-SpringBoot整合high-level-client - 127、全文检索-ElasticSearch-整合-测试复杂检索
Api的调用参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html
@SpringBootTest class GulimallProductApplicationTests { @Qualifier("esRestClient") @Autowired(required = false) private RestHighLevelClient client; @Data @ToString 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; } @Test void searchData() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("bank"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //构造检索条件 searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill")); //按照年龄进行聚合 TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("ageAgg").field("age").size(10); searchSourceBuilder.aggregation(termsAggregationBuilder); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("balanceAgg").field("balance"); searchSourceBuilder.aggregation(avgAggregationBuilder); searchRequest.source(searchSourceBuilder); System.out.println(searchSourceBuilder.toString()); //2。执行构造条件 SearchResponse searchResponse = client.search(searchRequest,GulimallElasticSearchConfig.COMMON_OPTIONS); //分析结果 System.out.println(searchResponse.toString()); //拿到响应的结果 SearchHits hits = searchResponse.getHits(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { String sourceAsString = hit.getSourceAsString(); Account account = JSON.parseObject(sourceAsString, Account.class); System.out.println(account); } //获取集合的信息 Aggregations aggregations = searchResponse.getAggregations(); Terms terms = aggregations.get("ageAgg"); for (Terms.Bucket bucket : terms.getBuckets()) { System.out.println("年龄:"+bucket.getKeyAsString()); } Avg avg = aggregations.get("balanceAgg"); double value = avg.getValue(); System.out.println(value); // Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic"); // Avg averageAge = elasticBucket.getAggregations().get("average_age"); // double avg = averageAge.getValue(); } @Test void indexData() throws IOException { IndexRequest request = new IndexRequest("users"); request.id("1"); User u = new User(); u.setAge(11); u.setGender("男"); u.setUserName("wgr"); String s = JSON.toJSONString(u); request.source(s, XContentType.JSON); IndexResponse indexResponse = client.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS); System.out.println(indexResponse); } @Data class User{ private String userName; private String gender; private Integer age; } @Test void contextLoads() { System.out.println(client); } }
结果:
{"query":{"match":{"address":{"query":"mill","operator":"OR","prefix_length":0,"max_expansions":50,"fuzzy_transpositions":true,"lenient":false,"zero_terms_query":"NONE","auto_generate_synonyms_phrase_query":true,"boost":1.0}}},"aggregations":{"ageAgg":{"terms":{"field":"age","size":10,"min_doc_count":1,"shard_min_doc_count":0,"show_term_doc_count_error":false,"order":[{"_count":"desc"},{"_key":"asc"}]}},"balanceAgg":{"avg":{"field":"balance"}}}} {"took":4,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":5.4032025,"hits":[{"_index":"bank","_type":"account","_id":"970","_score":5.4032025,"_source":{"account_number":970,"balance":19648,"firstname":"Forbes","lastname":"Wallace","age":28,"gender":"M","address":"990 Mill Road","employer":"Pheast","email":"forbeswallace@pheast.com","city":"Lopezo","state":"AK"}},{"_index":"bank","_type":"account","_id":"136","_score":5.4032025,"_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"winnieholland@neteria.com","city":"Urie","state":"IL"}},{"_index":"bank","_type":"account","_id":"345","_score":5.4032025,"_source":{"account_number":345,"balance":9812,"firstname":"Parker","lastname":"Hines","age":38,"gender":"M","address":"715 Mill Avenue","employer":"Baluba","email":"parkerhines@baluba.com","city":"Blackgum","state":"KY"}},{"_index":"bank","_type":"account","_id":"472","_score":5.4032025,"_source":{"account_number":472,"balance":25571,"firstname":"Lee","lastname":"Long","age":32,"gender":"F","address":"288 Mill Street","employer":"Comverges","email":"leelong@comverges.com","city":"Movico","state":"MT"}}]},"aggregations":{"lterms#ageAgg":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":38,"doc_count":2},{"key":28,"doc_count":1},{"key":32,"doc_count":1}]},"avg#balanceAgg":{"value":25208.0}}} GulimallProductApplicationTests.Account(account_number=970, balance=19648, firstname=Forbes, lastname=Wallace, age=28, gender=M, address=990 Mill Road, employer=Pheast, email=forbeswallace@pheast.com, city=Lopezo, state=AK) GulimallProductApplicationTests.Account(account_number=136, balance=45801, firstname=Winnie, lastname=Holland, age=38, gender=M, address=198 Mill Lane, employer=Neteria, email=winnieholland@neteria.com, city=Urie, state=IL) GulimallProductApplicationTests.Account(account_number=345, balance=9812, firstname=Parker, lastname=Hines, age=38, gender=M, address=715 Mill Avenue, employer=Baluba, email=parkerhines@baluba.com, city=Blackgum, state=KY) GulimallProductApplicationTests.Account(account_number=472, balance=25571, firstname=Lee, lastname=Long, age=32, gender=F, address=288 Mill Street, employer=Comverges, email=leelong@comverges.com, city=Movico, state=MT) 年龄:38 年龄:28 年龄:32 25208.0
原文:https://www.cnblogs.com/dalianpai/p/13149521.html