package com.xueyi.common.core.utils.sql;

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 lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yk
 * @description
 * @date 2023-08-29 15:55
 */
@Data
@Configuration
public class EsClient {
    @Value("${spring.elasticsearch.host}")
    public String esHost;

    @Value("${spring.elasticsearch.port}")
    public String esPort;


    @Bean
    public ElasticsearchClient getEsClient() {
        RestClient restClient = RestClient.builder(
                new HttpHost(esHost, Integer.valueOf(esPort))).build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }

}
