Search in sources :

Example 11 with ElasticsearchException

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

the class ElasticSearchTypeImpl method get.

@Override
public Optional<T> get(GetRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    int hits = 0;
    try {
        GetResponse<T> response = elasticSearch.client.get(builder -> builder.index(index).id(request.id), documentClass);
        if (!response.found())
            return Optional.empty();
        hits = 1;
        return Optional.of(response.source());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, hits, 0);
        logger.debug("get, 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 12 with ElasticsearchException

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

the class ElasticSearchTypeImpl method analyze.

@Override
public List<String> analyze(AnalyzeRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    try {
        AnalyzeResponse response = elasticSearch.client.indices().analyze(builder -> builder.index(index).analyzer(request.analyzer).text(request.text));
        return response.tokens().stream().map(AnalyzeToken::token).toList();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed);
        logger.debug("analyze, index={}, analyzer={}, elapsed={}", index, request.analyzer, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) AnalyzeResponse(co.elastic.clients.elasticsearch.indices.AnalyzeResponse) 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