|
@@ -2,9 +2,16 @@ package io.github.qifan777.knowledge.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 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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
+import org.elasticsearch.client.RestClientBuilder;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
@@ -20,11 +27,30 @@ import org.springframework.context.annotation.Configuration;
|
|
|
public class ElasticSearchConf {
|
|
|
@Bean
|
|
|
public ElasticsearchClient elasticSearchClient() {
|
|
|
- RestClient restClient=RestClient.builder(new HttpHost("58.87.69.234", 9200))
|
|
|
- .build();
|
|
|
+ // 1. 配置账户密码
|
|
|
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
+ credentialsProvider.setCredentials(
|
|
|
+ AuthScope.ANY,
|
|
|
+ new UsernamePasswordCredentials("elastic", "ASHkFLZem3XThWKD") // 替换为实际密码
|
|
|
+ );
|
|
|
|
|
|
- RestClientTransport restClientTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
|
|
|
+ // 2. 构建 RestClient 并注入认证信息
|
|
|
+ RestClientBuilder restClientBuilder = RestClient.builder(
|
|
|
+ new HttpHost("58.87.69.234", 9200)
|
|
|
+ ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
|
|
|
+ @Override
|
|
|
+ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
|
|
|
+ return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- return new ElasticsearchClient(restClientTransport);
|
|
|
+ // 3. 创建 Transport 和 Client
|
|
|
+ RestClient restClient = restClientBuilder.build();
|
|
|
+ ElasticsearchTransport transport = new RestClientTransport(
|
|
|
+ restClient,
|
|
|
+ new JacksonJsonpMapper()
|
|
|
+ );
|
|
|
+
|
|
|
+ return new ElasticsearchClient(transport);
|
|
|
}
|
|
|
}
|