Skip to content

Commit

Permalink
Merge commit '4e4e8586b744bc371db774f2b7115647003d2965' into feature/…
Browse files Browse the repository at this point in the history
…persistence_refactor
  • Loading branch information
Doris Lam committed Feb 13, 2024
2 parents 2c0daeb + 4e4e858 commit f5ef049
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.openmbee.mms.elastic.config;

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.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,11 +24,35 @@ public class ElasticsearchConfig {
@Value("${elasticsearch.http}")
private String elasticsearchHttp;

@Value("${elasticsearch.password}")
private String elasticsearchPassword;
@Value("${elasticsearch.username}")
private String elasticsearchUsername;

@Bean(name = "clientElastic", destroyMethod = "close")
public RestHighLevelClient restClient() {



RestClientBuilder builder = RestClient.builder(new HttpHost(elasticsearchHost, elasticsearchPort, elasticsearchHttp));
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(10000).setSocketTimeout(1000000));

if (elasticsearchPassword != null && elasticsearchUsername != null && !elasticsearchPassword.isEmpty() && !elasticsearchUsername.isEmpty()) {
final CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(elasticsearchUsername, elasticsearchPassword));
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider);
}
});
}


RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
Expand Down
6 changes: 5 additions & 1 deletion elastic/src/main/resources/application.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ elasticsearch.limit.result=100
elasticsearch.limit.term=100
elasticsearch.limit.scrollTimeout=1000
elasticsearch.limit.get=100000
elasticsearch.limit.index=5000
elasticsearch.limit.index=5000

#Optional Elasticsearch Basic Authentication Credentials
elasticsearch.username=
elasticsearch.password=
2 changes: 2 additions & 0 deletions example/src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spring.main.allow-circular-references=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

#Configuration for Elasticsearch
# elasticsearch.username=
# elasticsearch.password=
elasticsearch.host=elasticsearch
elasticsearch.port=9200
elasticsearch.http=http
Expand Down
4 changes: 4 additions & 0 deletions example/src/main/resources/application.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ elasticsearch.limit.get=100000
elasticsearch.limit.index=5000
elasticsearch.limit.commit=100000

#optional Elasticsearch Basic Authentication Credentials
elasticsearch.username=
elasticsearch.password=

#Configuration for TWC
#port is for REST interface
#aliases are for clustered usages
Expand Down

0 comments on commit f5ef049

Please sign in to comment.