use of core.framework.search.BulkIndexRequest 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);
}
}
Aggregations