use of co.elastic.clients.elasticsearch.ElasticsearchClient in project core-ng-project by neowu.
the class ElasticSearchImpl method initialize.
// initialize will be called in startup hook, no need to synchronize
public void initialize() {
if (client == null) {
// initialize can be called by initSearch explicitly during test,
RestClientBuilder builder = RestClient.builder(hosts);
builder.setRequestConfigCallback(config -> config.setSocketTimeout((int) timeout.toMillis()).setConnectionRequestTimeout(// timeout of requesting connection from connection pool
(int) timeout.toMillis()));
builder.setHttpClientConfigCallback(config -> config.setMaxConnTotal(100).setMaxConnPerRoute(100).setKeepAliveStrategy((response, context) -> Duration.ofSeconds(30).toMillis()));
builder.setHttpClientConfigCallback(config -> config.addInterceptorFirst(new ElasticSearchLogInterceptor()));
restClient = builder.build();
client = new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper(JSONMapper.OBJECT_MAPPER)));
}
}
use of co.elastic.clients.elasticsearch.ElasticsearchClient in project syncope by apache.
the class ElasticsearchClientFactoryBean method getObject.
@Override
public ElasticsearchClient getObject() throws Exception {
synchronized (this) {
if (client == null) {
RestClientBuilder builder = RestClient.builder(hosts.toArray(HttpHost[]::new));
if (username != null && password != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
builder.setHttpClientConfigCallback(b -> b.setDefaultCredentialsProvider(credentialsProvider));
} else if (serviceToken != null) {
builder.setDefaultHeaders(new Header[] { new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + serviceToken) });
} else if (apiKeyId != null && apiKeySecret != null) {
String apiKeyAuth = Base64.getEncoder().encodeToString((apiKeyId + ":" + apiKeySecret).getBytes(StandardCharsets.UTF_8));
builder.setDefaultHeaders(new Header[] { new BasicHeader(HttpHeaders.AUTHORIZATION, "ApiKey " + apiKeyAuth) });
}
restClient = builder.build();
client = new ElasticsearchClient(new RestClientTransport(restClient, new JacksonJsonpMapper()));
}
}
return client;
}
Aggregations