|
|
@@ -0,0 +1,80 @@
|
|
|
+package com.demo.seecanalysisbackend.config;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
|
|
+import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
|
|
|
+import org.springframework.data.elasticsearch.support.HttpHeaders;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class ElasticsearchConfig extends ElasticsearchConfiguration {
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.uris}")
|
|
|
+ private String uris;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.username:}")
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.password:}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.client.compatibility-mode:true}")
|
|
|
+ private boolean compatibilityMode;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.client.socket-timeout:${spring.elasticsearch.socket-timeout:60s}}")
|
|
|
+ private Duration socketTimeout;
|
|
|
+
|
|
|
+ @Value("${spring.elasticsearch.client.connection-timeout:${spring.elasticsearch.connection-timeout:5s}}")
|
|
|
+ private Duration connectionTimeout;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ClientConfiguration clientConfiguration() {
|
|
|
+ String[] hosts = Arrays.stream(uris.split(","))
|
|
|
+ .map(String::trim)
|
|
|
+ .filter(s -> !s.isEmpty())
|
|
|
+ .map(this::toHostPort)
|
|
|
+ .toArray(String[]::new);
|
|
|
+
|
|
|
+ ClientConfiguration.TerminalClientConfigurationBuilder builder = ClientConfiguration.builder()
|
|
|
+ .connectedTo(hosts)
|
|
|
+ .withConnectTimeout(connectionTimeout)
|
|
|
+ .withSocketTimeout(socketTimeout)
|
|
|
+ .withDefaultHeaders(defaultHeaders());
|
|
|
+
|
|
|
+ if (username != null && !username.isBlank()) {
|
|
|
+ return builder.withBasicAuth(username, password == null ? "" : password).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpHeaders defaultHeaders() {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ if (compatibilityMode) {
|
|
|
+ headers.add("Accept", "application/vnd.elasticsearch+json;compatible-with=7");
|
|
|
+ headers.add("Content-Type", "application/vnd.elasticsearch+json;compatible-with=7");
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ headers.add("Accept", "application/json");
|
|
|
+ headers.add("Content-Type", "application/json");
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String toHostPort(String uriText) {
|
|
|
+ URI uri = URI.create(uriText);
|
|
|
+ int port = uri.getPort();
|
|
|
+ if (port < 0) {
|
|
|
+ if ("https".equalsIgnoreCase(uri.getScheme())) {
|
|
|
+ port = 443;
|
|
|
+ } else {
|
|
|
+ port = 9200;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return uri.getHost() + ":" + port;
|
|
|
+ }
|
|
|
+}
|