Search in sources :

Example 1 with ForEach

use of core.framework.search.ForEach 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)

Aggregations

ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 Result (co.elastic.clients.elasticsearch._types.Result)1 Time (co.elastic.clients.elasticsearch._types.Time)1 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)1 DeleteResponse (co.elastic.clients.elasticsearch.core.DeleteResponse)1 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)1 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)1 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)1 CompletionSuggestOption (co.elastic.clients.elasticsearch.core.search.CompletionSuggestOption)1 Hit (co.elastic.clients.elasticsearch.core.search.Hit)1 Suggester (co.elastic.clients.elasticsearch.core.search.Suggester)1 AnalyzeResponse (co.elastic.clients.elasticsearch.indices.AnalyzeResponse)1 AnalyzeToken (co.elastic.clients.elasticsearch.indices.analyze.AnalyzeToken)1 JsonData (co.elastic.clients.json.JsonData)1 CodeBuilder (core.framework.internal.asm.CodeBuilder)1 Validator (core.framework.internal.validate.Validator)1 ActionLogContext (core.framework.log.ActionLogContext)1 Markers.errorCode (core.framework.log.Markers.errorCode)1 AnalyzeRequest (core.framework.search.AnalyzeRequest)1