• springboot使用ElasticSearch


    springboot使用ElasticSearch

    使用ES官方自带的java客户端(Java High Level REST Client)进行操作,不使用spring data 封装好的模板。
    因为没有人比ES官方更了解自己的产品

    1. 导入依赖包
            <!--Elastic Search 客户端  start-->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.6.2</version>
            </dependency>
            <!--如果elasticsearch-rest-high-level-client包中依赖的elasticsearch等版本不一致,需要手动引入相同版本-->
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>7.6.2</version>
            </dependency>
            <!--Elastic Search 客户端  end-->
    
    1. 创建application.yml 配置文件
    elasticsearch:
      hostList: localhost:9200 #多个节点用逗号隔开
    
    1. 添加配置类
    @Component
    @PropertySource("classpath:application.yml")//配置文件地址,可以自定义
    @ConfigurationProperties("elasticsearch")//属性前缀
    public class ElasticSearchConfig {
        //    @Value("${elasticsearch.hostList}")
        private String hostList;//配置文件中的属性
    
        public String getHostList() {
            return hostList;
        }
    
        public void setHostList(String hostList) {
            this.hostList = hostList;
        }
    
        @Bean(value = "RestHighLevelClient", destroyMethod = "close")
        public RestHighLevelClient restHighLevelClient() {
            //通过逗号分割节点
            String[] split = hostList.split(",");
            HttpHost[] httpHosts = new HttpHost[split.length];
            for (int i = 0; i < split.length; i++) {
                //通过冒号分离出每一个节点的ip,port
                String[] split1 = split[i].split(":");
                //这里http写固定了,只为测试使用,可以通过读取配置文件赋值的方式优化
                httpHosts[i] = new HttpHost(split1[0], Integer.parseInt(split1[1]), "http");
            }
            return new RestHighLevelClient(RestClient.builder(httpHosts));
        }
    }
    
    1. 在springboot test中测试CURD操作,具体查看 官方文档
    @SpringBootTest
    public class ElasticTest {
    
        @Autowired
        @Qualifier("RestHighLevelClient")
        private RestHighLevelClient client;
        /**
         * 添加数据
         * @throws IOException
         */
        @Test
        public void index() throws IOException {
            UserTest userTest = new UserTest();
            userTest.setName("董28");
            userTest.setSex("男");
            //由于客户端不支持自定义实体对象类作为添加数据的参数,所以提前先将对象转化为Map对象
            Map map = entityToMap(userTest);
            System.out.println(map);
            IndexRequest indexRequest = new IndexRequest("posts")
                    .source(map);
            //异步
            client.indexAsync(indexRequest, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() {
    
                @Override
                public void onResponse(IndexResponse indexResponse) {
                    System.out.println(indexResponse);
                    if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                        //新建
                    } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
                        //修改
                    }
                    ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
                    if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
                        //
                    }
                    if (shardInfo.getFailed() > 0) {
                        for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                            String reason = failure.reason();
                        }
                    }
    
                }
    
                @Override
                public void onFailure(Exception e) {
                    System.out.println("exception...");
                }
            });
            //同步
            /*IndexRequest indexRequest = new IndexRequest("posts")
                    .id("3").source(map);
            IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(index);*/
    
        }
    
        /**
         * 通过索引index,id查询
         * 
         */
        @Test
        public void get() throws IOException {
            GetRequest getRequest = new GetRequest("posts", "1");
            /*FetchSourceContext fetchSourceContext = new FetchSourceContext(true, new String[]{"sex"}, Strings.EMPTY_ARRAY);
            getRequest.fetchSourceContext(fetchSourceContext)*/
            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
            if (getResponse.isExists()) {
                System.out.println(getResponse);
                System.out.println(getResponse.getId());
                System.out.println(getResponse.getSource());
                System.out.println(getResponse.getSourceAsMap());
                UserTest userTest1 = entityConvert(getResponse.getSourceAsString(), UserTest.class);
                System.out.println(userTest1);
                UserTest userTest2 = entityConvert(getResponse.getSource(), UserTest.class);
                System.out.println(userTest2);
            }
        }
    
        @Test
        public void update() throws IOException {
            UpdateRequest updateRequest = new UpdateRequest("posts", "8");
            updateRequest.doc("sex", "女");
            UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
            System.out.println(update);
        }
    
        @Test
        public void delete() throws IOException {
            DeleteRequest deleteRequest = new DeleteRequest("posts");
            DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        }
    
        /**
         * 通过字段值查询
         * @throws IOException
         */
        @Test
        public void search() throws IOException {
            SearchRequest searchRequest = new SearchRequest("posts");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //通过匹配某一字段查询
    //        searchSourceBuilder.query(QueryBuilders.termQuery("name","董"));
            //选出指定结果字段
            searchSourceBuilder.fetchSource(new String[]{"id"},Strings.EMPTY_ARRAY);
            //对结果排序
    //        searchSourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.DESC));
            //聚集结果
            searchSourceBuilder.aggregation(
                    AggregationBuilders
                            .max("maxValue")//命名
                            .field("id"));//指定聚集的字段
            //查询所有
    //        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //从0开始
    //        searchSourceBuilder.from(2).size(2);
    
            searchRequest.source(searchSourceBuilder);
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(search);
        }
    
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        private static class UserTest {
            private int id;
            private String name;
            private String sex;
            private Others others = new Others("132@qq.com", "199");
        }
    
        @Data
        @AllArgsConstructor
        private static class Others {
            private String email;
            private String phone;
        }
    
        /**
         * 实体对象转化为map
         * @param object
         * @return
         * @throws JsonProcessingException
         */
        static Map entityToMap(Object object) throws JsonProcessingException {
            Map map = entityConvert(object, HashMap.class);
            return map;
        }
    
        /**
         * 一个对象转化为另一个有相同属性的对象
         * @param object
         * @param clazz
         * @param <T>
         * @return
         * @throws JsonProcessingException
         */
        static <T> T entityConvert(Object object, Class<T> clazz) throws JsonProcessingException {
            ObjectMapper objectMapper = new ObjectMapper();
            String s;
            if (object instanceof String) {
                s = String.valueOf(object);
            } else {
                s = objectMapper.writeValueAsString(object);
            }
            T t = objectMapper.readValue(s, clazz);
            return t;
        }
    }
    
  • 相关阅读:
    JS元素分组统计
    并发工具类
    【Spring-Cloud】mall eureka微服务模块建立
    【Spring-Cloud】mall父工程建立
    【MySQL】创建函数和存储过程,批量插入大数据
    【JAVA】http 状态
    【JAVA】如何写好代码
    【Docker】dockerfile,支持jdk8、 sshd、 python3.6
    【Hadoop】MapperReduce WordCount 代码示例
    【Hadoop】YARN 完全分布式配置
  • 原文地址:https://www.cnblogs.com/yloved/p/12888138.html
Copyright © 2020-2023  润新知