1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client

前言

在Elasticsearch7.15版本之后,Elasticsearch官方将它的高级客户端RestHighLevelClient标记为弃用状态。同时推出了全新的Java API客户端Elasticsearch Java API Client,该客户端也将在Elasticsearch8.0及以后版本中成为官方推荐使用的客户端。

Elasticsearch Java API Client 支持除 Vector tile search API 和 Find structure API 之外的所有 Elasticsearch API。且支持所有API数据类型,并且不再有原始JsonValue属性。它是针对Elasticsearch8.0及之后版本的客户端,目前Elasticsearch已经更新至8.1.1,所以我们需要学习新的Elasticsearch Java API Client的使用方法。

新建项目

设置maven

1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client_第1张图片

设置编码

1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client_第2张图片

注解激活生效

1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client_第3张图片

Java版本选择

1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client_第4张图片

文件过滤

1、初始ElasticSearch-最新版Spring Boot整合ElasticSearch 8.0.1使用最新的Elasticsearch Java API Client_第5张图片

pom文件添加相关依赖


        
            co.elastic.clients
            elasticsearch-java
            8.1.1
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        

        
        
            jakarta.json
            jakarta.json-api
            2.0.1
        
        

Maven Shade插件的配置。将以下内容添加到pom中:


    
        
            org.apache.maven.plugins
            maven-shade-plugin
            3.1.0
            
                
                    package
                    shade
                    
                        
                            
                                org.apache.http
                                hidden.org.apache.http
                            
                            
                                org.apache.logging
                                hidden.org.apache.logging
                            
                            
                                org.apache.commons.codec
                                hidden.org.apache.commons.codec
                            
                            
                                org.apache.commons.logging
                                hidden.org.apache.commons.logging
                            
                        
                    
                
            
        
    

完整pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
         
    
    com.itcoding
    esapi
    0.0.1-SNAPSHOT
    itcoding-esapi
    itcoding-esapi
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            co.elastic.clients
            elasticsearch-java
            8.1.1
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        

        
        
            jakarta.json
            jakarta.json-api
            2.0.1
        
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    



添加ElasticSearchClientConfig类

package com.itcoding.esapi.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;


@ConfigurationProperties(prefix = "elasticsearch")//配置配置信息的前缀
@Configuration
public class ElasticSearchClientConfig {
    /**
     * 多个IP逗号隔开
     */
    private String hosts;

    @Bean
    public RestClient restClient() {
        HttpHost[] httpHosts = toHttpHost();
        
        RestClient restClient = RestClient.builder(httpHosts).build();
        
        return restClient;
    }


    /**
     * 解析hosts
     * @return
     */
    private HttpHost[] toHttpHost() {
        if (!StringUtils.hasLength(hosts)) {
            throw new RuntimeException("elasticsearch.hosts不能为空!");
        }
        String[] hostArray = hosts.split(",");
        HttpHost[] httpHosts = new HttpHost[hostArray.length];
        HttpHost httpHost;
        for (int i = 0; i < hostArray.length; i++) {
            String[] strings = hostArray[i].split(":");
            httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "http");
            httpHosts[i] = httpHost;
        }

        return httpHosts;
    }


    public String getHosts() {
        return hosts;
    }

    public void setHosts(String hosts) {
        this.hosts = hosts;
    }
}

添加配置信息

elasticsearch.hosts=10.211.55.11:9200

创建索引

@SpringBootTest
class ItcodingEsapiApplicationTests {

    @Resource
    private ElasticsearchClient elasticsearchClient;

    @Test
    void createIndex() throws IOException {
        CreateIndexResponse indexResponse = elasticsearchClient.indices().create(c -> c.index("itcoding-test"));
        System.out.println(indexResponse.index());
    }

}

持续更新中…

你可能感兴趣的:(elasticsearch,spring,boot)