use of org.elasticsearch.action.bulk.BulkRequestBuilder in project metacat by Netflix.
the class ElasticSearchUtilImpl method bulkSaveToIndex.
/**
* Bulk save of the entities.
*
* @param type index type
* @param docs metacat documents
*/
private void bulkSaveToIndex(final String type, final List<ElasticSearchDoc> docs, final String index) {
if (docs != null && !docs.isEmpty()) {
try {
RETRY_ES_PUBLISH.call(() -> {
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (ElasticSearchDoc doc : docs) {
final IndexRequestBuilder indexRequestBuilder = prepareIndexRequest(index, type, doc);
if (indexRequestBuilder != null) {
bulkRequest.add(indexRequestBuilder);
}
}
if (bulkRequest.numberOfActions() > 0) {
final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
log.info("Bulk saving metadata of index {} type {} with size {}.", index, type, docs.size());
if (bulkResponse.hasFailures()) {
for (BulkItemResponse item : bulkResponse.getItems()) {
if (item.isFailed()) {
log.error("Failed saving metadata of {} index type {} with id {}. Message: {}", index, type, item.getId(), item.getFailureMessage());
this.elasticSearchMetric.getElasticSearchSaveFailureCounter().increment();
log("ElasticSearchUtil.bulkSaveToIndex.index", type, item.getId(), null, item.getFailureMessage(), null, true, index);
}
}
}
}
return null;
});
} catch (Exception e) {
log.error("Failed saving metadatas of index {} type {}. {}", index, type, e);
this.elasticSearchMetric.getElasticSearchBulkSaveFailureCounter().increment();
final List<String> docIds = docs.stream().map(ElasticSearchDoc::getId).collect(Collectors.toList());
log("ElasticSearchUtil.bulkSaveToIndex", type, docIds.toString(), null, e.getMessage(), e, true, index);
}
}
}
use of org.elasticsearch.action.bulk.BulkRequestBuilder in project metacat by Netflix.
the class ElasticSearchUtilImpl method softDeleteDoc.
/* Use elasticSearch bulk API to mark the documents as deleted
* @param type index type
* @param ids list of entity ids
* @param metacatRequestContext context containing the user name
*/
private void softDeleteDoc(final String type, final List<String> ids, final MetacatRequestContext metacatRequestContext) {
try {
RETRY_ES_PUBLISH.call(() -> {
final BulkRequestBuilder bulkRequest = client.prepareBulk();
final XContentBuilder builder = XContentFactory.contentBuilder(contentType);
builder.startObject().field(ElasticSearchDoc.Field.DELETED, true).field(ElasticSearchDoc.Field.TIMESTAMP, java.time.Instant.now().toEpochMilli()).field(ElasticSearchDoc.Field.USER, metacatRequestContext.getUserName()).endObject();
ids.forEach(id -> bulkRequest.add(client.prepareUpdate(esIndex, type, id).setRetryOnConflict(NO_OF_CONFLICT_RETRIES).setDoc(builder)));
final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
if (bulkResponse.hasFailures()) {
for (BulkItemResponse item : bulkResponse.getItems()) {
if (item.isFailed()) {
log.error("Failed soft deleting metadata of type {} with id {}. Message: {}", type, item.getId(), item.getFailureMessage());
this.elasticSearchMetric.getElasticSearchDeleteFailureCounter().increment();
log("ElasticSearchUtil.bulkSoftDelete.item", type, item.getId(), null, item.getFailureMessage(), null, true);
}
}
}
return null;
});
} catch (Exception e) {
log.error("Failed soft deleting metadata of type {} with ids {}. {}", type, ids, e);
this.elasticSearchMetric.getElasticSearchBulkDeleteFailureCounter().increment();
log("ElasticSearchUtil.bulkSoftDelete", type, ids.toString(), null, e.getMessage(), e, true);
}
}
Aggregations