Search in sources :

Example 1 with ElasticsearchException

use of co.elastic.clients.elasticsearch._types.ElasticsearchException in project syncope by apache.

the class ElasticsearchIndexManager method createIndex.

public void createIndex(final String domain, final AnyTypeKind kind, final IndexSettings settings, final TypeMapping mappings) throws IOException {
    try {
        CreateIndexResponse response = doCreateIndex(domain, kind, settings, mappings);
        LOG.debug("Successfully created {} for {}: {}", ElasticsearchUtils.getContextDomainName(domain, kind), kind.name(), response);
    } catch (ElasticsearchException e) {
        LOG.debug("Could not create index {} because it already exists", ElasticsearchUtils.getContextDomainName(domain, kind), e);
        removeIndex(domain, kind);
        doCreateIndex(domain, kind, settings, mappings);
    }
}
Also used : ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse)

Example 2 with ElasticsearchException

use of co.elastic.clients.elasticsearch._types.ElasticsearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method update.

@Override
public void update(UpdateRequest<T> request) {
    var watch = new StopWatch();
    if (request.script == null)
        throw new Error("request.script must not be null");
    String index = request.index == null ? this.index : request.index;
    try {
        Map<String, JsonData> params = request.params == null ? Map.of() : request.params.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, value -> JsonData.of(value.getValue())));
        elasticSearch.client.update(builder -> builder.index(index).id(request.id).script(s -> s.inline(i -> i.source(request.script).params(params))), documentClass);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, 0, 1);
        logger.debug("update, index={}, id={}, script={}, elapsed={}", index, request.id, request.script, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : AnalyzeResponse(co.elastic.clients.elasticsearch.indices.AnalyzeResponse) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) CompletionSuggestOption(co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption) GetRequest(core.framework.search.GetRequest) CodeBuilder(core.framework.internal.asm.CodeBuilder) ActionLogContext(core.framework.log.ActionLogContext) LoggerFactory(org.slf4j.LoggerFactory) UpdateRequest(core.framework.search.UpdateRequest) Index(core.framework.search.Index) BulkIndexRequest(core.framework.search.BulkIndexRequest) IndexRequest(core.framework.search.IndexRequest) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) DeleteResponse(co.elastic.clients.elasticsearch.core.DeleteResponse) BulkDeleteRequest(core.framework.search.BulkDeleteRequest) Markers.errorCode(core.framework.log.Markers.errorCode) CompleteRequest(core.framework.search.CompleteRequest) Duration(java.time.Duration) Map(java.util.Map) SearchException(core.framework.search.SearchException) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) Time(co.elastic.clients.elasticsearch._types.Time) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) Validator(core.framework.internal.validate.Validator) Logger(org.slf4j.Logger) JsonData(co.elastic.clients.json.JsonData) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) Collection(java.util.Collection) Suggester(co.elastic.clients.elasticsearch.core.search.Suggester) IOException(java.io.IOException) AnalyzeToken(co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Result(co.elastic.clients.elasticsearch._types.Result) UncheckedIOException(java.io.UncheckedIOException) DeleteRequest(core.framework.search.DeleteRequest) Hit(co.elastic.clients.elasticsearch.core.search.Hit) ElasticSearchType(core.framework.search.ElasticSearchType) List(java.util.List) AnalyzeRequest(core.framework.search.AnalyzeRequest) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) BulkResponseItem(co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem) Strings(core.framework.util.Strings) Optional(java.util.Optional) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) JsonData(co.elastic.clients.json.JsonData) StopWatch(core.framework.util.StopWatch)

Example 3 with ElasticsearchException

use of co.elastic.clients.elasticsearch._types.ElasticsearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method bulkIndex.

@Override
public void bulkIndex(BulkIndexRequest<T> request) {
    var watch = new StopWatch();
    if (request.sources == null || request.sources.isEmpty())
        throw new Error("request.sources must not be empty");
    String index = request.index == null ? this.index : request.index;
    List<BulkOperation> operations = new ArrayList<>(request.sources.size());
    for (Map.Entry<String, T> entry : request.sources.entrySet()) {
        String id = entry.getKey();
        T source = entry.getValue();
        validator.validate(source, false);
        operations.add(BulkOperation.of(builder -> builder.index(i -> i.index(index).id(id).document(source))));
    }
    long esTook = 0;
    try {
        BulkResponse response = elasticSearch.client.bulk(builder -> builder.operations(operations));
        // mills to nano
        esTook = response.took() * 1_000_000;
        validate(response);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, 0, request.sources.size());
        logger.debug("bulkIndex, index={}, size={}, esTook={}, elapsed={}", index, request.sources.size(), esTook, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : AnalyzeResponse(co.elastic.clients.elasticsearch.indices.AnalyzeResponse) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) CompletionSuggestOption(co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption) GetRequest(core.framework.search.GetRequest) CodeBuilder(core.framework.internal.asm.CodeBuilder) ActionLogContext(core.framework.log.ActionLogContext) LoggerFactory(org.slf4j.LoggerFactory) UpdateRequest(core.framework.search.UpdateRequest) Index(core.framework.search.Index) BulkIndexRequest(core.framework.search.BulkIndexRequest) IndexRequest(core.framework.search.IndexRequest) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) DeleteResponse(co.elastic.clients.elasticsearch.core.DeleteResponse) BulkDeleteRequest(core.framework.search.BulkDeleteRequest) Markers.errorCode(core.framework.log.Markers.errorCode) CompleteRequest(core.framework.search.CompleteRequest) Duration(java.time.Duration) Map(java.util.Map) SearchException(core.framework.search.SearchException) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) Time(co.elastic.clients.elasticsearch._types.Time) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) Validator(core.framework.internal.validate.Validator) Logger(org.slf4j.Logger) JsonData(co.elastic.clients.json.JsonData) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) Collection(java.util.Collection) Suggester(co.elastic.clients.elasticsearch.core.search.Suggester) IOException(java.io.IOException) AnalyzeToken(co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Result(co.elastic.clients.elasticsearch._types.Result) UncheckedIOException(java.io.UncheckedIOException) DeleteRequest(core.framework.search.DeleteRequest) Hit(co.elastic.clients.elasticsearch.core.search.Hit) ElasticSearchType(core.framework.search.ElasticSearchType) List(java.util.List) AnalyzeRequest(core.framework.search.AnalyzeRequest) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) BulkResponseItem(co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem) Strings(core.framework.util.Strings) Optional(java.util.Optional) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) ArrayList(java.util.ArrayList) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch) Map(java.util.Map)

Example 4 with ElasticsearchException

use of co.elastic.clients.elasticsearch._types.ElasticsearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method index.

@Override
public void index(IndexRequest<T> request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    validator.validate(request.source, false);
    try {
        elasticSearch.client.index(builder -> builder.index(index).id(request.id).document(request.source));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, 0, 1);
        logger.debug("index, index={}, id={}, elapsed={}", index, request.id, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 5 with ElasticsearchException

use of co.elastic.clients.elasticsearch._types.ElasticsearchException in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method bulkDelete.

@Override
public void bulkDelete(BulkDeleteRequest request) {
    var watch = new StopWatch();
    if (request.ids == null || request.ids.isEmpty())
        throw new Error("request.ids must not be empty");
    String index = request.index == null ? this.index : request.index;
    List<BulkOperation> operations = new ArrayList<>(request.ids.size());
    for (String id : request.ids) {
        operations.add(BulkOperation.of(builder -> builder.delete(r -> r.index(index).id(id))));
    }
    long esTook = 0;
    try {
        BulkResponse response = elasticSearch.client.bulk(builder -> builder.operations(operations));
        // mills to nano
        esTook = response.took() * 1_000_000;
        validate(response);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        int size = request.ids.size();
        ActionLogContext.track("elasticsearch", elapsed, 0, size);
        logger.debug("bulkDelete, index={}, ids={}, size={}, esTook={}, elapsed={}", index, request.ids, size, esTook, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : AnalyzeResponse(co.elastic.clients.elasticsearch.indices.AnalyzeResponse) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) CompletionSuggestOption(co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption) GetRequest(core.framework.search.GetRequest) CodeBuilder(core.framework.internal.asm.CodeBuilder) ActionLogContext(core.framework.log.ActionLogContext) LoggerFactory(org.slf4j.LoggerFactory) UpdateRequest(core.framework.search.UpdateRequest) Index(core.framework.search.Index) BulkIndexRequest(core.framework.search.BulkIndexRequest) IndexRequest(core.framework.search.IndexRequest) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) DeleteResponse(co.elastic.clients.elasticsearch.core.DeleteResponse) BulkDeleteRequest(core.framework.search.BulkDeleteRequest) Markers.errorCode(core.framework.log.Markers.errorCode) CompleteRequest(core.framework.search.CompleteRequest) Duration(java.time.Duration) Map(java.util.Map) SearchException(core.framework.search.SearchException) SearchRequest(core.framework.search.SearchRequest) SearchResponse(core.framework.search.SearchResponse) ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) Time(co.elastic.clients.elasticsearch._types.Time) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) Validator(core.framework.internal.validate.Validator) Logger(org.slf4j.Logger) JsonData(co.elastic.clients.json.JsonData) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) Collection(java.util.Collection) Suggester(co.elastic.clients.elasticsearch.core.search.Suggester) IOException(java.io.IOException) AnalyzeToken(co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken) ForEach(core.framework.search.ForEach) Collectors(java.util.stream.Collectors) Result(co.elastic.clients.elasticsearch._types.Result) UncheckedIOException(java.io.UncheckedIOException) DeleteRequest(core.framework.search.DeleteRequest) Hit(co.elastic.clients.elasticsearch.core.search.Hit) ElasticSearchType(core.framework.search.ElasticSearchType) List(java.util.List) AnalyzeRequest(core.framework.search.AnalyzeRequest) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) BulkResponseItem(co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem) Strings(core.framework.util.Strings) Optional(java.util.Optional) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) ArrayList(java.util.ArrayList) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Aggregations

ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)11 StopWatch (core.framework.util.StopWatch)10 IOException (java.io.IOException)10 UncheckedIOException (java.io.UncheckedIOException)10 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)7 DeleteResponse (co.elastic.clients.elasticsearch.core.DeleteResponse)7 AnalyzeResponse (co.elastic.clients.elasticsearch.indices.AnalyzeResponse)7 SearchException (core.framework.search.SearchException)7 Result (co.elastic.clients.elasticsearch._types.Result)6 Time (co.elastic.clients.elasticsearch._types.Time)6 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)6 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)6 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)6 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)6 CompletionSuggestOption (co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption)6 Hit (co.elastic.clients.elasticsearch.core.search.Hit)6 Suggester (co.elastic.clients.elasticsearch.core.search.Suggester)6 AnalyzeToken (co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken)6 JsonData (co.elastic.clients.json.JsonData)6 CodeBuilder (core.framework.internal.asm.CodeBuilder)6