springboot集成elasticsearch

版本信息

springboot集成es会有版本要求。
springboot:2.2.6
es:6.4.0

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

有三种方式:

百度查得:
Spring Boot更新到2.x版本, 默认spring-boot-starter-data-elasticsearch 默认的ES版本为5.6.9;如果你仍然使用Spring Boot 1.x版本,那么默认的Elastisearch版本为2.x ( https://www.jianshu.com/p/c5c3e834c028

spring官网
https://docs.spring.io/spring-data/elasticsearch/docs/3.2.0.RC3/reference/html/#preface.versions

high-level-client

pom添加依赖

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

需要一个 RestClient 的 bean,在application里添加或者用 @Component

public class TiebaApplication {

    public static void main(String[] args) {
        SpringApplication.run(TiebaApplication.class, args);
    }

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.2.202", 9200, "http")));
        return client;
    }

    @Bean
    public ExecutorService pool(){
        ExecutorService pool =  Executors.newFixedThreadPool(10);
        return pool;
    }

}

使用(官网api: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high.html

posted @ 2020-04-19 21:06  少年留不住  阅读(473)  评论(0编辑  收藏  举报