|
@@ -0,0 +1,38 @@
|
|
|
+package com.bowintek.practice.config;
|
|
|
+
|
|
|
+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 co.elastic.clients.util.ContentType;
|
|
|
+import org.apache.http.HttpHeaders;
|
|
|
+import org.apache.http.HttpHost;
|
|
|
+import org.apache.http.HttpResponseInterceptor;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.elasticsearch.client.RestClient;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class ElasticsearchConfig {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ EsConfig config;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ElasticsearchClient esClient() {
|
|
|
+ RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort()))
|
|
|
+ .setHttpClientConfigCallback(httpClientBuilder
|
|
|
+ ->httpClientBuilder.setDefaultHeaders(
|
|
|
+ List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
|
|
|
+ .addInterceptorLast((HttpResponseInterceptor) (response, context)
|
|
|
+ -> response.addHeader("X-Elastic-Product", "Elasticsearch"))).build();
|
|
|
+ ElasticsearchTransport transport = new RestClientTransport(
|
|
|
+ restClient, new JacksonJsonpMapper());
|
|
|
+ ElasticsearchClient client = new ElasticsearchClient(transport);
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+}
|