Search in sources :

Example 1 with ElasticsearchClient

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)));
    }
}
Also used : RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) RestClient(org.elasticsearch.client.RestClient) JSONMapper(core.framework.internal.json.JSONMapper) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) Logger(org.slf4j.Logger) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) HttpEntity(org.apache.http.HttpEntity) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Request(org.elasticsearch.client.Request) StopWatch(core.framework.util.StopWatch) EntityUtils(org.apache.http.util.EntityUtils) UncheckedIOException(java.io.UncheckedIOException) ElasticSearchType(core.framework.search.ElasticSearchType) ElasticsearchIndicesClient(co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient) ElasticSearch(core.framework.search.ElasticSearch) Response(org.elasticsearch.client.Response) Duration(java.time.Duration) ClusterStateResponse(core.framework.search.ClusterStateResponse) SearchException(core.framework.search.SearchException) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient)

Example 2 with ElasticsearchClient

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;
}
Also used : RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)2 JacksonJsonpMapper (co.elastic.clients.json.jackson.JacksonJsonpMapper)2 RestClientTransport (co.elastic.clients.transport.rest_client.RestClientTransport)2 HttpHost (org.apache.http.HttpHost)2 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)2 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 ElasticsearchIndicesClient (co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient)1 JSONMapper (core.framework.internal.json.JSONMapper)1 ClusterStateResponse (core.framework.search.ClusterStateResponse)1 ElasticSearch (core.framework.search.ElasticSearch)1 ElasticSearchType (core.framework.search.ElasticSearchType)1 SearchException (core.framework.search.SearchException)1 StopWatch (core.framework.util.StopWatch)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Duration (java.time.Duration)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1