zoukankan      html  css  js  c++  java
  • 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集成完毕。

  • 相关阅读:
    scrapy下载图片第一波
    scrapy之防ban策略
    xml之dtd约束
    Java jdbc相关
    XSS获取cookie
    反射型XSS & DVWA
    MySQL数据库设计规范
    SQL Server 查询指定时间是一年当中的第几周
    js验证15位或18位身份证
    C# Process.Start()
  • 原文地址:https://www.cnblogs.com/richardz/p/12626401.html
Copyright © 2011-2022 走看看