Search in sources :

Example 1 with ElasticsearchIndicesClient

use of co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient in project core-ng-project by neowu.

the class ElasticSearchImpl method putIndex.

// this is generally used in es migration, to create index or update mapping if index exists, be aware of mapping fields can't be deleted in es, but can be removed from mapping json
@Override
public void putIndex(String index, String source) {
    var watch = new StopWatch();
    HttpEntity entity = null;
    try {
        ElasticsearchIndicesClient client = this.client.indices();
        boolean exists = client.exists(builder -> builder.index(index)).value();
        Request request;
        if (!exists) {
            request = new Request("PUT", "/" + index);
            request.setJsonEntity(source);
        } else {
            // only try to update mappings, as for settings it generally requires closing index first then open after update
            logger.info("index already exists, update mapping, index={}", index);
            request = new Request("PUT", "/" + index + "/_mapping");
            request.setJsonEntity(JSONMapper.OBJECT_MAPPER.readTree(source).get("mappings").toString());
        }
        Response response = restClient.performRequest(request);
        entity = response.getEntity();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        EntityUtils.consumeQuietly(entity);
        logger.info("put index, index={}, source={}, elapsed={}", index, source, watch.elapsed());
    }
}
Also used : ElasticsearchIndicesClient(co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient) 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) Response(org.elasticsearch.client.Response) ClusterStateResponse(core.framework.search.ClusterStateResponse) HttpEntity(org.apache.http.HttpEntity) Request(org.elasticsearch.client.Request) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Aggregations

ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)1 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 ElasticsearchIndicesClient (co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient)1 JacksonJsonpMapper (co.elastic.clients.json.jackson.JacksonJsonpMapper)1 RestClientTransport (co.elastic.clients.transport.rest_client.RestClientTransport)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 HttpEntity (org.apache.http.HttpEntity)1 HttpHost (org.apache.http.HttpHost)1 EntityUtils (org.apache.http.util.EntityUtils)1 Request (org.elasticsearch.client.Request)1 Response (org.elasticsearch.client.Response)1