Search in sources :

Example 6 with ElasticsearchException

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

the class ElasticSearchTypeImpl method complete.

@Override
public List<String> complete(CompleteRequest request) {
    var watch = new StopWatch();
    long esTook = 0;
    String index = request.index == null ? this.index : request.index;
    int options = 0;
    try {
        var suggest = Suggester.of(builder -> {
            builder.text(request.prefix);
            for (String field : request.fields) {
                builder.suggesters(field, s -> s.completion(c -> c.field(field).skipDuplicates(Boolean.TRUE).size(request.limit)));
            }
            return builder;
        });
        var response = elasticSearch.client.search(builder -> builder.index(index).suggest(suggest).source(s -> s.fetch(Boolean.FALSE)), documentClass);
        esTook = response.took() * 1_000_000;
        List<String> suggestions = response.suggest().values().stream().flatMap(Collection::stream).flatMap(suggestion -> suggestion.completion().options().stream()).map(CompletionSuggestOption::text).distinct().collect(Collectors.toList());
        options = suggestions.size();
        return suggestions;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, options, 0);
        logger.debug("complete, options={}, esTook={}, elapsed={}", options, 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) Collection(java.util.Collection) UncheckedIOException(java.io.UncheckedIOException) CompletionSuggestOption(co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch)

Example 7 with ElasticsearchException

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

the class ElasticSearchImpl method searchException.

// convert elasticsearch-java client exception, to append detailed error message
SearchException searchException(ElasticsearchException e) {
    ErrorCause causedBy = e.error().causedBy();
    var builder = new StringBuilder(e.getMessage());
    if (causedBy != null) {
        builder.append("\nreason: ").append(causedBy.reason());
    }
    return new SearchException(builder.toString(), e);
}
Also used : ErrorCause(co.elastic.clients.elasticsearch._types.ErrorCause) SearchException(core.framework.search.SearchException)

Example 8 with ElasticsearchException

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

the class ElasticSearchTypeImpl method forEach.

@Override
public void forEach(ForEach<T> forEach) {
    var watch = new StopWatch();
    long start = System.nanoTime();
    long esClientTook = 0;
    long esServerTook = 0;
    validate(forEach);
    Time keepAlive = Time.of(t -> t.time(forEach.scrollTimeout.toNanos() + "nanos"));
    String index = forEach.index == null ? this.index : forEach.index;
    int totalHits = 0;
    try {
        var response = elasticSearch.client.search(builder -> builder.index(index).scroll(keepAlive).query(forEach.query).sort(s -> s.field(f -> f.field("_doc"))).size(forEach.limit), documentClass);
        var holder = new ScrollIdHolder();
        holder.scrollId = response.scrollId();
        while (true) {
            esServerTook += response.took() * 1_000_000;
            var hits = response.hits().hits();
            esClientTook += System.nanoTime() - start;
            if (hits.isEmpty())
                break;
            totalHits += hits.size();
            for (var hit : hits) {
                forEach.consumer.accept(hit.source());
            }
            start = System.nanoTime();
            response = elasticSearch.client.scroll(builder -> builder.scrollId(holder.scrollId).scroll(keepAlive), documentClass);
            holder.scrollId = response.scrollId();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, totalHits, 0);
        logger.debug("forEach, totalHits={}, esServerTook={}, esClientTook={}, elapsed={}", totalHits, esServerTook, esClientTook, 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) Time(co.elastic.clients.elasticsearch._types.Time) 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 9 with ElasticsearchException

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

the class ElasticSearchTypeImpl method delete.

@Override
public boolean delete(DeleteRequest request) {
    var watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    boolean deleted = false;
    try {
        DeleteResponse response = elasticSearch.client.delete(builder -> builder.index(index).id(request.id));
        deleted = response.result() == Result.Deleted;
        return deleted;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (ElasticsearchException e) {
        throw elasticSearch.searchException(e);
    } finally {
        long elapsed = watch.elapsed();
        ActionLogContext.track("elasticsearch", elapsed, 0, deleted ? 1 : 0);
        logger.debug("delete, index={}, id={}, elapsed={}", index, request.id, elapsed);
        checkSlowOperation(elapsed);
    }
}
Also used : DeleteResponse(co.elastic.clients.elasticsearch.core.DeleteResponse) 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 10 with ElasticsearchException

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

the class ElasticSearchTypeImpl method search.

@Override
public SearchResponse<T> search(SearchRequest request) {
    var watch = new StopWatch();
    validate(request);
    long esTook = 0;
    String index = request.index == null ? this.index : request.index;
    int hits = 0;
    try {
        var searchRequest = co.elastic.clients.elasticsearch.core.SearchRequest.of(builder -> {
            builder.index(index).query(request.query).aggregations(request.aggregations).sort(request.sorts).searchType(request.type).from(request.skip).size(request.limit);
            if (request.trackTotalHitsUpTo != null)
                builder.trackTotalHits(t -> t.count(request.trackTotalHitsUpTo));
            return builder;
        });
        var response = elasticSearch.client.search(searchRequest, documentClass);
        esTook = response.took() * 1_000_000;
        hits = response.hits().hits().size();
        long total = response.hits().total().value();
        List<T> items = response.hits().hits().stream().map(Hit::source).toList();
        return new SearchResponse<>(items, total, response.aggregations());
    } 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("search, hits={}, esTook={}, elapsed={}", hits, 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) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) StopWatch(core.framework.util.StopWatch) SearchResponse(core.framework.search.SearchResponse)

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