• Spring Boot 集成ElasticSearch


    1.添加maven依赖

    以6.8.8版本为例

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>6.8.7</version>
    </dependency>
    

    2.添加配置文件

    @Configuration
    public class ElasticSearchConfigurer {
    
        @Value("${elasticsearch.host}")
        String host;
    
        @Autowired
        private RestClientBuilder restClientBuilder;
    
        @Bean
        public RestClientBuilder restClientBuilder() {
    
            return RestClient.builder(getHost(host));
        }
    
    
        @Bean
        public RestHighLevelClient restHighLevelClient() {
    
            return new RestHighLevelClient(restClientBuilder);
        }
    
    
        private HttpHost[] getHost(String s) {
            String[] addresses = s.split(",");
            HttpHost[] result = new HttpHost[addresses.length];
            for (int i = 0; i < addresses.length; i++) {
                String[] address = addresses[i].split(":");
                String ip = address[0];
                int port = Integer.parseInt(address[1]);
                result[i] = new HttpHost(ip, port, "http");
            }
    
            return result;
        }
    

    3.在yml中添加ES地址

    elasticsearch:
      host: 192.168.3.207:9200
    

    如果有多个以,分隔

    elasticsearch:
      host: 192.168.3.207:9200,192.168.3.208:9200
    

    4.使用

    在需要使用地方注入RestHighLevelClient 即可

     @Autowired
     private RestHighLevelClient restHighLevelClient;
    

    至此ElasticSearch集成完毕。

  • 相关阅读:
    day12_字符连接单引号转意字符
    day12_存储过程说明
    day12_PLSQL编程--存储过程---统一发布动态属性管理
    linux关闭celinux服务
    day11__表管理
    day11_分区表------子分区的母模板(11g)
    day11_分区表------子分区的母模板(10g)
    day11_分区表——分区表常用维护
    smartforms 中的currquan单位处理
    当SVN服务器端IP地址发生变化时,客户端重新定位
  • 原文地址:https://www.cnblogs.com/richardz/p/12626401.html
Copyright © 2020-2023  润新知