springboot集成elasticSearch

前言

本人目前用的elasticSearch是8.12.2的版本,springboot用的版本是2.6.2的版本,在集成的时候,出现

maven依赖

这个问题,解决办法升级springboot的版本到3.0,降低elasticSearch的版本,目前由于项目需要,暂时降低版本,但记录下本次尝试的过程

xml 复制代码
<!--    <dependency>-->
<!--      <groupId>org.elasticsearch.client</groupId>-->
<!--      <artifactId>elasticsearch-rest-client</artifactId>-->
<!--      <version>8.12.2</version>-->
<!--    </dependency>-->

    <dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.12.2</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.17.0</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>jakarta.json</artifactId>
      <version>2.0.1</version>
    </dependency>

配置

java 复制代码
package org.example.es;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfig {

    public static String url;

    public static String apiKey;



    @Value("${elasticsearch.url:localhost:9200}")
    public void setUrl(String url) {
        this.url = url;
    }


    @Value("${elasticsearch.apiKey:ZTNHZ1RwWUJtajlYVkszdmJ3YUo6ZXd4bUpGOHVUdnFvWmNEZ3JjWlgxdw==}")
    public void setApiKey(String apiKey) {
        ElasticsearchConfig.apiKey = apiKey;
    }
}
java 复制代码
package org.example.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientOptions;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.*;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class EsClient {

    private static ElasticsearchClient client;

    public  ElasticsearchClient getInstance() {

        if(client == null) {
            synchronized (this)  {

                RestClient restClient = RestClient
                        .builder(HttpHost.create(ElasticsearchConfig.url))
                        .setDefaultHeaders(new Header[]{
                                new BasicHeader("Authorization", "ApiKey " + ElasticsearchConfig.apiKey)
                        })
                        .build();

                ElasticsearchTransport transport = new RestClientTransport(
                        restClient, new JacksonJsonpMapper());

                client = new ElasticsearchClient(transport);
                return client;
            }

        }
        return client;
    }


}
java 复制代码
@Autowired
private EsClient esClient;


@PostMapping(value = "/esTest")
public void esTest() {

    ElasticsearchClient instance = esClient.getInstance();
    try {
        CreateIndexResponse createIndexResponse = instance.indices().create(c ->
                c.index("index_user_info")
                        .settings(s -> s.numberOfShards("1").numberOfReplicas("3"))
                        .mappings(m -> m.properties("id", p -> p.text(d -> d))
                                .properties("name", p -> p.text(t -> t.analyzer("ik_max_word")))
                                .properties("height", p -> p.double_(d -> d))
                                .properties("createTime", p -> p.date(d -> d.format("yyyy-MM-dd")))
                        )
        );
        boolean acknowledged = createIndexResponse.acknowledged();
        log.info("创建index_user_info索引返回结果:{}",acknowledged);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }


}
相关推荐
Moshow郑锴15 分钟前
Spring Boot 3 + Undertow 服务器优化配置
服务器·spring boot·后端
Chandler241 小时前
Go语言即时通讯系统 开发日志day1
开发语言·后端·golang
有梦想的攻城狮1 小时前
spring中的@Lazy注解详解
java·后端·spring
野犬寒鸦2 小时前
Linux常用命令详解(下):打包压缩、文本编辑与查找命令
linux·运维·服务器·数据库·后端·github
huohuopro2 小时前
thinkphp模板文件缺失没有报错/thinkphp无法正常访问控制器
后端·thinkphp
cainiao0806055 小时前
《Spring Boot 4.0新特性深度解析》
java·spring boot·后端
-曾牛5 小时前
Spring AI 与 Hugging Face 深度集成:打造高效文本生成应用
java·人工智能·后端·spring·搜索引擎·springai·deepseek
南玖yy6 小时前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计
计算机学姐6 小时前
基于SpringBoot的小区停车位管理系统
java·vue.js·spring boot·后端·mysql·spring·maven
BUG制造机.6 小时前
Go 语言 slice(切片) 的使用
开发语言·后端·golang