package com.storlead.es.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: sp-tems-gotr
 * @description:
 * @author: chenkq
 * @create: 2023-06-16 17:03
 */
@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {

        String hostname = "39.108.252.62";
        int port = 9200;
        // 定义 Elasticsearch 集群的用户名和密码
        String username = "elastic";
        String password = "storlead123456";
        // 配置 RestClient
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                                new HttpHost(hostname, port, "http"))
                        .setHttpClientConfigCallback(httpClientBuilder ->
                                httpClientBuilder.setDefaultCredentialsProvider(
                                        new BasicCredentialsProvider() {{
                                            setCredentials(AuthScope.ANY,
                                                    new UsernamePasswordCredentials(username, password));
                                        }}
                                )
                        )
        );
        return client;
    }
}
