java(springboot)对接elasticsearch8+

1、pom引用

注:jackson包es只用到了databind,之所以全部引用是因为actuator用到了其他,只升级一个会 导致版本冲突

java 复制代码
<!-- https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java -->
<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.8.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.15.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.15.2</version>
</dependency>
<dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.1.2</version>
</dependency>

2、配置文件

注:因为没有用springboot自身的es插件所以健康检查检测不到es状态,关闭es检测

java 复制代码
elasticsearch:
  host: 10.247.149.67
  port: 9200
  userName: elastic
  password: Zlens@2023
management:
  health:
    elasticsearch:
      enabled: false

3、连接池

java 复制代码
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.RestClientTransport;
import com.alibaba.cloud.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {

    @Value("${elasticsearch.host}")
    private String host;
    @Value("${elasticsearch.port}")
    private Integer port;
    @Value("${elasticsearch.apiKey:}")
    private String apiKey;
    @Value("${elasticsearch.userName}")
    private String userName;
    @Value("${elasticsearch.password}")
    private String password;


    @Bean
    public ElasticsearchClient elasticsearchClient() {
        ElasticsearchTransport transport;
        RestClient restClient;
        if(StringUtils.isNotBlank(apiKey)){
            //apiKey验证模式
            restClient = RestClient
                    .builder(new HttpHost(host, port))
                    .setDefaultHeaders(new Header[]{
                            new BasicHeader("Authorization", "ApiKey " + apiKey)})
                    .build();

        }else{
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            // 账号密码验证模式
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
            restClient = RestClient.builder(new HttpHost(host, port))
                    .setHttpClientConfigCallback(httpClientBuilder ->
                            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
        }
        transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
        System.out.println("es初始化完成");
        return client;
    }
}

4、操作类

java 复制代码
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.DateProperty;
import co.elastic.clients.elasticsearch._types.mapping.Property;
import co.elastic.clients.elasticsearch.core.BulkRequest;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class ElasticsearchService<T> {
    @Resource
    private ElasticsearchClient elasticsearchClient;

    /**
     * 判断索引是否存在.
     *
     * @param indexName index名称
     */
    public boolean existIndex(String indexName) {
        try {
            BooleanResponse booleanResponse = elasticsearchClient.indices().exists(e -> e.index(indexName));
            return booleanResponse.value();
        } catch (IOException e) {
            log.error("向es中检测索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
            throw new RuntimeException("向es中检测索引【"+indexName+"】出错");
        }
    }

    /**
     * 创建索引.
     *
     * @param indexName index名称
     */
    public void createIndex(String indexName) {
        try {
            Map<String, Property> documentMap = new HashMap<>();
            documentMap.put("createTime", Property.of(property ->
                            property.date(DateProperty.of(dateProperty ->
                                            dateProperty.index(true).format("epoch_millis"))
                                    //textProperty.index(true))
                            )
                    )
            );
            elasticsearchClient.indices().create(c -> c.index(indexName).mappings(mappings ->
                    mappings.properties(documentMap)));
        } catch (IOException e) {
            log.error("向es中创建索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
        }
    }

    /**
     * 添加记录.
     *
     */
    public void addDocument(T data, String indexName) {
        try {
            if (!this.existIndex(indexName)) {
                this.createIndex(indexName);
            }
            elasticsearchClient.index(i -> i.index(indexName).id(null).document(data));
        } catch (IOException e) {
            log.error("向es中添加document出错:{}", e.getMessage());
        }
    }

    /**
     * 批量添加.
     *
     * @param dataList 添加的数量集合
     * @param indexName indexName
     */
    public void batchAddDocument(List<T> dataList, String indexName) {
        if (!this.existIndex(indexName)) {
            this.createIndex(indexName);
        }

        BulkRequest.Builder br = new BulkRequest.Builder();
        dataList.forEach(data -> br.operations(op -> op
                .index(idx -> idx
                        .index(indexName)
                        .id(null)
                        .document(data)
                ))
        );

        try {
            BulkResponse result = elasticsearchClient.bulk(br.build());
            if (result.errors()) {
                log.error("Bulk had errors");
                for (BulkResponseItem item : result.items()) {
                    if (item.error() != null) {
                        log.error(item.error().reason());
                    }
                }
            }
        } catch (IOException e) {
            log.error("向es中添加document出错:{}",e.getMessage());
        }
    }

    /**
     * 根据索引名称和字段查询数据.
     *
     * @param indexName 索引名称
     * @param filedValue 查询字段值
     * @param filedName 查询字段名称
     */
    public List<T> findDataList(String indexName, String filedName, String filedValue,Class<T> className) {
        try {
            SearchResponse<T> searchResponse = elasticsearchClient.search(s -> s.index(indexName)
                            .query(q -> q
                                    .match(t -> t
                                            .field(filedName)
                                            .query(filedValue)
                                    )),
                    className);
            List<Hit<T>> hitList = searchResponse.hits().hits();
            List<T> dataList = new ArrayList<>();
            for (Hit<T> mapHit : hitList) {
                dataList.add(mapHit.source());
            }
            return dataList;
        } catch (IOException e) {
            log.error("【查询 -> 失败】从es中查询分析后的日志出错,错误信息为:{}", e.getMessage());
        }
        return null;
    }
}

上边创建索引是定制的加了特殊mapping,正常这样

java 复制代码
    /**
     * 创建索引.
     *
     * @param indexName index名称
     */
    public void createIndex(String indexName) {
        try {
            elasticsearchClient.indices().create(c -> c.index(indexName));
        } catch (IOException e) {
            log.error("向es中创建索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
        }
    }
相关推荐
狂放不羁霸24 分钟前
idea | 搭建 SpringBoot 项目之配置 Maven
spring boot·maven·intellij-idea
九圣残炎25 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge27 分钟前
Netty篇(入门编程)
java·linux·服务器
计算机学长felix1 小时前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友
Re.不晚1 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
雷神乐乐1 小时前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven
码农派大星。1 小时前
Spring Boot 配置文件
java·spring boot·后端
顾北川_野1 小时前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
江深竹静,一苇以航1 小时前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
confiself1 小时前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言