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);
    }


}
相关推荐
未完结小说30 分钟前
服务注册与发现(nacos)
后端
AI智能科技用户79463297836 分钟前
okcc呼叫中心两个sip对接线路外呼任务怎么设置才能一个任务对应yigesip中继?
人工智能·后端
懒虫虫~1 小时前
Spring源码中关于抽象方法且是个空实现这样设计的思考
java·后端·spring
雷渊1 小时前
DDD的分层架构是怎么样的?
后端
会有猫1 小时前
阿里云OSS挂载到Linux
后端
雷渊1 小时前
聊一聊贫血模型和充血模型区别
后端
瀚海澜生1 小时前
NSQ 深入剖析(一):架构原理与安装实战
后端
大鹏dapeng2 小时前
Gone 框架的服务注册与发现:打造高效微服务架构
后端·go·github
LemonDu2 小时前
线上救急-AWS限频
后端·算法·架构
AronTing2 小时前
装饰模式:动态扩展对象功能的优雅设计
java·后端·设计模式