-
org.elasticsearch.client -
elasticsearch-rest-high-level-client -
7.4.2
在spring-boot-dependencies中所依赖的ES版本位6.8.5,要改掉
-
1.8 -
7.4.2
官方建议把requestOptions创建成单实例
- package com.hdb.pingmoweb.search.config;
-
- import org.apache.http.HttpHost;
- import org.elasticsearch.client.RequestOptions;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.elasticsearch.client.RestHighLevelClient;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
-
- @Configuration
- public class PingmoESConfig {
- public static final RequestOptions COMMON_OPTIONS;
-
- static {
- RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
- COMMON_OPTIONS = builder.build();
- }
-
- @Bean
- public RestHighLevelClient esRestClient() {
- RestClientBuilder builder = null;
- // 可以指定多个es
- builder = RestClient.builder(new HttpHost("192.168.19.128", 9200, "http"));
- RestHighLevelClient client = new RestHighLevelClient(builder);
- return client;
- }
-
- }
官方文档:Index API | Java REST Client [7.17] | Elastic
Index API | Java REST Client [7.17] | Elastic
- @Test
- public void indexData() throws IOException {
-
- // 设置索引
- IndexRequest indexRequest = new IndexRequest ("users");
- indexRequest.id("1");
-
- User user = new User();
- user.setUserName("张三");
- user.setAge(20);
- user.setGender("男");
- String jsonString = JSON.toJSONString(user);
-
- //设置要保存的内容,指定数据和类型
- indexRequest.source(jsonString, XContentType.JSON);
-
- //执行创建索引和保存数据
- IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
-
- System.out.println(index);
-
- }
- @Test
- public void find() throws IOException {
- // 1 创建检索请求
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("bank");
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
- // 构造检索条件
- // sourceBuilder.query();
- // sourceBuilder.from();
- // sourceBuilder.size();
- // sourceBuilder.aggregation();
- sourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
- //AggregationBuilders工具类构建AggregationBuilder
- // 构建第一个聚合条件:按照年龄的值分布
- TermsAggregationBuilder agg1 = AggregationBuilders.terms("agg1").field("age").size(10);// 聚合名称
- // 参数为AggregationBuilder
- sourceBuilder.aggregation(agg1);
- // 构建第二个聚合条件:平均薪资
- AvgAggregationBuilder agg2 = AggregationBuilders.avg("agg2").field("balance");
- sourceBuilder.aggregation(agg2);
-
- System.out.println("检索条件"+sourceBuilder.toString());
-
- searchRequest.source(sourceBuilder);
-
- // 2 执行检索
- SearchResponse response = client.search(searchRequest, PingmoESConfig.COMMON_OPTIONS);
- // 3 分析响应结果
- System.out.println(response.toString());
- }
- {
- "took": 28,
- "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": {
- "avg#agg2": {
- "value": 25208.0
- },
- "lterms#agg1": {
- "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
- }]
- }
- }
- }
- private void parseSearchResponse(SearchResponse response){
- SearchHits hits = response.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 = response.getAggregations();
- Terms agg1 = aggregations.get("agg1");
- for(Terms.Bucket bucket:agg1.getBuckets()){
- String keyAsString = bucket.getKeyAsString();
- System.out.println("年龄:"+keyAsString);
- }
- Avg agg2 = aggregations.get("agg2");
- System.out.println("平均薪资:"+agg2.getValue());
- }