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);
}
}
Aggregations