elasticsearch 8.5.0 部署及springboot集成

3,273 阅读2分钟

es更新换代很快,目前已经升级到了最新的8.5.1版本,网上8.x相关的部署和应用文档还不全,在这里做个记录。

安装及部署

这里主要讲docker部署:

# 下载镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.5.0

# 运行es容器
docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:8.5.0

# 拷贝配置文件到服务器本地,进行修改,修改内容如下:
docker cp es:/usr/share/elasticsearch/config/elasticsearch.yml /data/elasticsearch/config/elasticsearch.yml

# elasticsearch.yml新增以下配置,开启跨域.
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization

# 开启xpack安全验证(默认已经开启了).
xpack.security.enabled: true

# 关闭https校验(我不需要,所以关了).
xpack.security.http.ssl:
  enabled: false

# 重启es
docker restart es

# 进入es容器内部,设置密码:
docker exec -it es /bin/bash 

cd bin

elasticsearch-setup-passwords interactive

# 然后依次输入密码,最后再重启es.
docker retart es

然后在浏览器中安装 Multi Elasticsearch Head 扩展程序,安装完后,添加http://ip:9200 即可输入用户名 elastic 以及刚才设置的密码进行登录,成功登录表示es安装及设置正常.

spring boot 集成

  1. pom文件中引入相关依赖
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.13.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.json</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>co.elastic.clients</groupId>
        <artifactId>elasticsearch-java</artifactId>
        <version>8.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>8.5.0</version>
    </dependency>
  1. yml文件中新增es的相关配置

    elasticsearch:
      host: es-ip
      port: 端口9200
      userName: elastic
      password: xxxx
    
  2. 配置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.RestClientTransport;
    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.elasticsearch.client.RestClient;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * es配置.
     *
     * @author missMubu
     * @createTime 2022-11-04
     * @since 3.1.0
     */
    @Configuration
    public class ElasticsearchConfig {
    
        @Value("${elasticsearch.host}")
        private String host;
    
        @Value("${elasticsearch.port}")
        private int port;
    
        @Value("${elasticsearch.userName}")
        private String userName;
    
        @Value("${elasticsearch.password}")
        private String password;
    
        /**
         * 初始化es-client.
         *
         * @return RestHighLevelClient
         */
        @Bean
        public ElasticsearchClient elasticsearchClient() {
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            // 设置账号密码.
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
            RestClient restClient = RestClient.builder(new HttpHost(host, port))
                        .setHttpClientConfigCallback(httpClientBuilder ->
                                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
            ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
            return new ElasticsearchClient(transport);
        }
    
    }
    
    

基本使用

/**
 * es service.
 *
 * @author missMubu
 * @createTime 2022-11-04
 * @since 3.1.0
 */
@Service
@Slf4j
public class ElasticsearchService {

    @Resource
    private ElasticsearchClient client;

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

    /**
     * 创建索引.
     *
     * @param indexName index名称
    */
    public void createIndex(String indexName) {
        try {
            client.indices().create(c -> c.index(indexName));
        } catch (IOException e) {
            throw new EsExcepotion("向es中创建索引【{}】出错,错误信息为:{}", indexName, e.getMessage());
        }
    }
    
    /**
     * 添加记录.
     *
     */
    public void addDocument(AppLog appLog, String indexName) {
        try {
            if (!this.existIndex(indexName)) {
                this.createIndex(indexName);
            }
            client.index(i -> i.index(indexName).id(appLog.getId()).document(appLog));
        } catch (IOException e) {
            throw new EsExcepotion("向es中添加dockument出错!", e.getMessage());
        }
    }
    
    /**
     * 批量添加.
     *
     * @param appLogList 添加的数量集合
     * @param indexName indexName
    */
    public void batchAddDocument(List<AppLog> appLogList, String indexName) {
        if (!this.existIndex(indexName)) {
            this.createIndex(indexName);
        }

        BulkRequest.Builder br = new BulkRequest.Builder();
        appLogList.forEach(appLog -> br.operations(op -> op
                .index(idx -> idx
                        .index(indexName)
                        .id(appLog.getId())
                        .document(appLog)
                ))
        );

        try {
            BulkResponse result = client.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) {
            throw new EsExcepotion(e, "向es中添加dockument出错");
        }
    }
    
      /**
     * 根据索引名称和字段查询数据.
     *
     * @param indexName 索引名称
     * @param filedValue 查询字段值
     * @param filedName 查询字段名称
    */
    public List<AppLog> findApplogs(String indexName, String filedName, String filedValue) {
        try {
            SearchResponse<AppLog> searchResponse = client.search(s -> s.index(indexName)
                            .query(q -> q
                                    .match(t -> t
                                            .field(filedName)
                                            .query(filedValue)
                                    )),
                    AppLog.class);
            List<Hit<AppLog>> hitList = searchResponse.hits().hits();
            List<AppLog> appLogList = new ArrayList<>();
            for (Hit<AppLog> mapHit : hitList) {
                appLogList.add(mapHit.source());
            }
            return appLogList;
        } catch (IOException e) {
            throw new EsExcepotion("【查询 -> 失败】从es中查询分析后的日志出错,错误信息为:{}", e.getMessage());
        }
    }
    
}

参考: Elasticsearch Java API Client 8.5