• ElasticSearch ( 六 ) 与SpringBoot整合


    6.与springboot整合

    https://www.elastic.co/guide/en/elasticsearch/client/java-rest/

    6.1.pom.xml 引入依赖

            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-elasticsearchartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4

    如果当前springboot所默认依赖的版本与es版本不相同

            <dependency>
                <groupId>org.elasticsearch.clientgroupId>
                <artifactId>elasticsearch-rest-high-level-clientartifactId>
                <version>7.15.0version>
                <exclusions>
                    <exclusion>
                        <groupId>org.elasticsearchgroupId>
                        <artifactId>elasticsearchartifactId>
                    exclusion>
                exclusions>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6.2.配置文件

    指明服务器的IP

    spring.elasticsearch.rest.uris=192.168.46.123:9200
    
    • 1

    6.3.es操作对象

        @Autowired
        private RestHighLevelClient client;
    
    • 1
    • 2

    6.4.操作

    6.4.1.实体类

    package com.yuan.estest.entity;
    
    import lombok.Data;
    
    @Data
    public class StoryEntity {
    
         private String storyTitle;
         private String storyAuthor;
         private String storyContentEn;
         private String storyContentCn;
         private int storyConut;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    package com.yuan.estest.entity;
    
    import lombok.Data;
    
    @Data
    public class AccountEntity {
    
        private Integer account_number;
        private Integer balance;
        private String firstname;
        private String lastname;
        private Integer age;
        private String gender;
        private String address;
        private String employer;
        private String email;
        private String city;
        private String state;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.4.2.Controller

    
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;
    import com.yuan.estest.entity.AccountEntity;
    import com.yuan.estest.entity.StoryEntity;
    import lombok.extern.slf4j.Slf4j;
    import org.elasticsearch.action.DocWriteRequest;
    import org.elasticsearch.action.bulk.BulkItemResponse;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.action.delete.DeleteResponse;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    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.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.Node;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    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.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    @Slf4j
    public class Test {
    
        @Autowired
        private RestHighLevelClient client;
    
        private String INDEX_NAME = "testdata";
    
        /***************DATA**********************/
    
        public List<StoryEntity> getDatasForList(){
            List<StoryEntity> list = new ArrayList<>();
            StoryEntity storyEntity1 = new StoryEntity();
            storyEntity1.setStoryTitle("笑看人生");
            storyEntity1.setStoryAuthor("开心");
            storyEntity1.setStoryContentEn("happy lift");
            storyEntity1.setStoryContentCn("一生无苦");
            storyEntity1.setStoryConut(10);
            list.add(storyEntity1);
    
            StoryEntity storyEntity2 = new StoryEntity();
            storyEntity2.setStoryTitle("华英雄");
            storyEntity2.setStoryAuthor("黄玉郞");
            storyEntity2.setStoryContentEn("hua hero");
            storyEntity2.setStoryContentCn("一个叫华英雄的英雄");
            storyEntity2.setStoryConut(1079867);
            list.add(storyEntity2);
    
            StoryEntity storyEntity3 = new StoryEntity();
            storyEntity3.setStoryTitle("英雄一生都欢乐");
            storyEntity3.setStoryAuthor("笑英雄");
            storyEntity3.setStoryContentEn("hero is happy");
            storyEntity3.setStoryContentCn("英雄一生都开心");
            storyEntity3.setStoryConut(1267340);
            list.add(storyEntity3);
    
    
            return list;
        }
    
    
    
        /**
         * 新增一条
         */
        @RequestMapping("/indexData")
        public void indexData() throws IOException {
    
    
    
            //信息
            StoryEntity storyEntity = new StoryEntity();
            storyEntity.setStoryTitle("英雄之歌");
            storyEntity.setStoryAuthor("流浪英雄");
            storyEntity.setStoryContentEn("hero's song");
            storyEntity.setStoryContentCn("一首英雄的赞歌");
            storyEntity.setStoryConut(1267340);
            // 将对象转为json
            String data = JSONObject.toJSONString(storyEntity);
    
            IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
            indexRequest.id("7");
            // 保存, 指明类型
            indexRequest.source(data, XContentType.JSON);
            IndexRequest timeout = indexRequest.timeout("50s");
            System.out.println("timeout = " + timeout);
            // 执行
            IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
            // 获取响应数据
            log.info("创建状态:{}", response.status());
        }
    
    
    
        /**
         * 获取文档信息
         */
        public void getDocument() throws IOException {
            // 创建获取请求对象
            GetRequest getRequest = new GetRequest(INDEX_NAME, "7");
            GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
            System.out.println(response.getSourceAsString());
        }
    
    
    
        /**
         * 批量新增
         */
        @RequestMapping("/syncData")
        public String syncData() {
    
            //批量数据
            List<StoryEntity> list = getDatasForList();
    
            // 1.创建Request
            BulkRequest request = new BulkRequest();
    
            //下面尽量控制一下一次bulk的数量,如果数据过大,条数过多可能出现同步不完全的情况
            for (StoryEntity storyEntity : list) {
    
                String jsonString = JSONObject.toJSONString(storyEntity);
                IndexRequest indexRequest = new IndexRequest(INDEX_NAME);
                indexRequest.source(jsonString, XContentType.JSON);
                // 保存, 指明类型
                request.add(indexRequest);
            }
            try {
                BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
                System.out.println("response = " + response);
    
                if (response.hasFailures()) {
                    exceptionRetry(request, response);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return "over";
        }
    
        /**
         * 不重要
         * 异常捕获用于重试
         */
        private void exceptionRetry(BulkRequest request, BulkResponse response) {
            List<DocWriteRequest<?>> list = request.requests();
            BulkRequest requestRetry = new BulkRequest();
            //下面尽量控制一下一次bulk的数量,如果数据过大,条数过多可能出现同步不完全的情况
            for (BulkItemResponse bir : response) {
                if (bir.isFailed()) {
                    int docIndex = bir.getItemId();
                    IndexRequest ir = (IndexRequest) list.get(docIndex);
                    requestRetry.add(new IndexRequest(INDEX_NAME).source(ir.sourceAsMap(), XContentType.JSON));
                }
            }
            try {
                //遇到错误,休眠1s后重试
                Thread.sleep(1000);
                BulkResponse responseRetry = client.bulk(requestRetry, RequestOptions.DEFAULT);
                //重试仍然失败时记录该数据
                exceptionLog(requestRetry, responseRetry);
            } catch (Exception e) {
                log.error("ES同步重试出错!", e);
            }
        }
    
        /**
         * 不重要
         * 重试结果判断
         */
        private void exceptionLog(BulkRequest request, BulkResponse response) {
            List<DocWriteRequest<?>> list = request.requests();
            for (BulkItemResponse bir : response) {
                if (bir.isFailed()) {
                    int docIndex = bir.getItemId();
                    IndexRequest ir = (IndexRequest) list.get(docIndex);
                    //记录失败原因及失败数据
                    log.error("同步重试失败reason=[{}],data=[{}]", bir.getFailureMessage(), ir.sourceAsMap().toString());
                }
            }
        }
    
    
    
    
    
        /**************************************修改*******************************************/
        /**
         * 根据查询将数据更新,唯一主键
         *         "_id" : "7",
         *         "_score" : 1.0,
         *         "_source" : {
         *           "storyTitle" : "英雄故事",
         *           "storyAuthor" : "不是真龙",
         *           "storyContentEn" : "Hero story",
         *           "storyContentCn" : "英雄故事",
         *           "storyConut" : 1234
         *         }
         */
        @RequestMapping("/updateById")
        public String updateById() {
    
            StoryEntity storyEntity = new StoryEntity();
            storyEntity.setStoryTitle("英雄故事");
            storyEntity.setStoryAuthor("不是真龙也不是英雄");
            storyEntity.setStoryContentEn("Hero story");
            storyEntity.setStoryContentCn("英雄故事");
            storyEntity.setStoryConut(1234);
    
            if (log.isDebugEnabled()) {
                log.info("es开始更新数据:{}", JSON.toJSONString(storyEntity));
            }
    
            // 创建索引请求对象
            UpdateRequest request ;
            try {
                 String data = JSONObject.toJSONString(storyEntity);
                request = new UpdateRequest(INDEX_NAME, "7").doc(data, XContentType.JSON);
                UpdateResponse response = client.update(request,  RequestOptions.DEFAULT);
                log.info("更新状态:{}", response.getResult());
            } catch (IOException e) {
                log.error("更新写入异常:{}", e.getMessage(), e);
            }
            if (log.isDebugEnabled()) {
                log.info("es更新数据完成");
            }
            return "over";
        }
    
        /**
         * 删除文档信息
         */
        public void deleteDocument() throws IOException {
    
            // 创建删除请求对象
            DeleteRequest deleteRequest = new DeleteRequest(INDEX_NAME, "7");
            // 执行删除文档
            DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
            log.info("删除状态:{}", response.status());
        }
    
    
    //------------------------------------------------------------------------
    
        /**
         * 获取文档信息
         */
        @RequestMapping("/query")
        public void query() throws IOException {
            // 1.准备Request
            SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
            // 2.准备DSL
            searchRequest.source().query(QueryBuilders.matchAllQuery());
            // 3.发送请求
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println("searchResponse = " + searchResponse);
    
        }
    
    
    
        @RequestMapping("/queryBySearch")
        public void queryBySearch() throws IOException {
    
            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices("bank");
    
            // 设置 查询条件
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.matchQuery("address", "mill"));
    
            TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
            sourceBuilder.aggregation(ageAgg);
    
            AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
            sourceBuilder.aggregation(balanceAvg);
    
            System.out.println("1:==>sourceBuilder.toString() = " + sourceBuilder);
            searchRequest.source(sourceBuilder);
    
            // 分析结果
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println("2:==>searchResponse = " + searchResponse);
    
    
            // 查询结果
            SearchHits searchHits = searchResponse.getHits();
            System.out.println("hits.getTotalHits() = " + searchHits.getTotalHits());
    
            SearchHit[] hitss = searchHits.getHits();
            for (SearchHit hit : hitss) {
                String sourceAsString = hit.getSourceAsString();
                System.out.println("sourceAsString = " + sourceAsString);
                AccountEntity accountEntity = JSONObject.parseObject(sourceAsString, new TypeReference<AccountEntity>() {});
                System.out.println("accountEntity = " + accountEntity);
            }
    
            // 聚合信息
            Aggregations aggregations = searchResponse.getAggregations();
    
            Terms ageAggData = aggregations.get("ageAgg");
            for (Terms.Bucket bucket : ageAggData.getBuckets()) {
                System.out.println("bucket.getKeyAsString() = " + bucket.getKeyAsString());
            }
            Avg balanceAvgData = aggregations.get("balanceAvg");
            System.out.println("balanceAvgData.getValue() = " + balanceAvgData.getValue());
        }
    
    
        @RequestMapping("/testES")
        public void testES() throws IOException {
            List<Node> nodes = client.getLowLevelClient().getNodes();
            nodes.forEach(node -> {
                System.out.println(node.getHost());
            });
            System.out.println(client);
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
  • 相关阅读:
    可编程控制器有几种编程语言以及它们的特点
    拼搏半个月,刷了 571道Java高频面试题喜提阿里 offer
    数学建模--Subplot绘图的Python实现
    安卓请求权限
    央国企、金融信创改造必备的Windows AD域控国产替代方案
    SpringBoot和SpringCloud版本对应
    吴恩达机器学习课程资源(笔记、中英文字幕视频、课后作业,提供百度云镜像!)
    大前端CPU优化技术--NEON自动向量化
    判断素数/质数的快速算法
    多线程的相关知识
  • 原文地址:https://blog.csdn.net/yuanchun05/article/details/127781671