Search in sources :

Example 1 with JacksonJsonpMapper

use of co.elastic.clients.json.jackson.JacksonJsonpMapper 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 JacksonJsonpMapper

use of co.elastic.clients.json.jackson.JacksonJsonpMapper in project micronaut-elasticsearch by micronaut-projects.

the class DefaultElasticsearchClientFactory method elasticsearchTransport.

/**
 * @param elasticsearchConfiguration The {@link DefaultElasticsearchConfigurationProperties} object.
 * @param objectMapper The {@link ObjectMapper} object.
 * @return The {@link ElasticsearchTransport}.
 * @since 4.2.0
 */
@Singleton
@Bean(preDestroy = "close")
ElasticsearchTransport elasticsearchTransport(DefaultElasticsearchConfigurationProperties elasticsearchConfiguration, ObjectMapper objectMapper) {
    RestClient restClient = restClientBuilder(elasticsearchConfiguration).build();
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper(objectMapper));
    return transport;
}
Also used : ElasticsearchTransport(co.elastic.clients.transport.ElasticsearchTransport) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) RestClient(org.elasticsearch.client.RestClient) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Example 3 with JacksonJsonpMapper

use of co.elastic.clients.json.jackson.JacksonJsonpMapper 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

JacksonJsonpMapper (co.elastic.clients.json.jackson.JacksonJsonpMapper)3 RestClientTransport (co.elastic.clients.transport.rest_client.RestClientTransport)3 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)2 HttpHost (org.apache.http.HttpHost)2 RestClient (org.elasticsearch.client.RestClient)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 ElasticsearchTransport (co.elastic.clients.transport.ElasticsearchTransport)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 Bean (io.micronaut.context.annotation.Bean)1 Singleton (jakarta.inject.Singleton)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1