use of org.elasticsearch.action.bulk.BulkItemResponse 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
*/
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.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());
registry.counter(registry.createId(Metrics.CounterElasticSearchDelete.name()).withTags(Metrics.statusFailureMap)).increment();
log("ElasticSearchUtil.bulkSoftDelete.item", type, item.getId(), null, item.getFailureMessage(), null, true);
}
}
}
return null;
});
} catch (Exception e) {
log.error(String.format("Failed soft deleting metadata of type %s with ids %s", type, ids), e);
registry.counter(registry.createId(Metrics.CounterElasticSearchBulkDelete.name()).withTags(Metrics.statusFailureMap)).increment();
log("ElasticSearchUtil.bulkSoftDelete", type, ids.toString(), null, e.getMessage(), e, true);
}
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project samza by apache.
the class ElasticsearchSystemProducerTest method getRespWithFailedDocument.
private BulkResponse getRespWithFailedDocument(RestStatus status) {
BulkResponse response = mock(BulkResponse.class);
when(response.hasFailures()).thenReturn(true);
BulkItemResponse itemResp = mock(BulkItemResponse.class);
when(itemResp.isFailed()).thenReturn(true);
BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
when(failure.getStatus()).thenReturn(status);
when(itemResp.getFailure()).thenReturn(failure);
BulkItemResponse[] itemResponses = new BulkItemResponse[] { itemResp };
when(response.getItems()).thenReturn(itemResponses);
return response;
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project fess by codelibs.
the class FessEsClient method addAll.
public void addAll(final String index, final String type, final List<Map<String, Object>> docList) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
for (final Map<String, Object> doc : docList) {
final Object id = doc.remove(fessConfig.getIndexFieldId());
bulkRequestBuilder.add(client.prepareIndex(index, type, id.toString()).setSource(new DocMap(doc)));
}
final BulkResponse response = bulkRequestBuilder.execute().actionGet(ComponentUtil.getFessConfig().getIndexBulkTimeout());
if (response.hasFailures()) {
if (logger.isDebugEnabled()) {
@SuppressWarnings("rawtypes") final List<DocWriteRequest> requests = bulkRequestBuilder.request().requests();
final BulkItemResponse[] items = response.getItems();
if (requests.size() == items.length) {
for (int i = 0; i < requests.size(); i++) {
final BulkItemResponse resp = items[i];
if (resp.isFailed() && resp.getFailure() != null) {
final DocWriteRequest<?> req = requests.get(i);
final Failure failure = resp.getFailure();
logger.debug("Failed Request: " + req + "\n=>" + failure.getMessage());
}
}
}
}
throw new FessEsClientException(response.buildFailureMessage());
}
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project fess by codelibs.
the class EsAbstractBehavior method delegateBatchRequest.
protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
@SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
final BulkRequestBuilder bulkBuilder = client.prepareBulk();
for (final Entity entity : entityList) {
final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
BUILDER builder = call.apply(esEntity);
if (builder instanceof IndexRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((IndexRequestBuilder) builder);
} else if (builder instanceof UpdateRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((UpdateRequestBuilder) builder);
} else if (builder instanceof DeleteRequestBuilder) {
if (builderEntityCall != null) {
builderEntityCall.callback(builder);
}
bulkBuilder.add((DeleteRequestBuilder) builder);
}
}
final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
if (builderCall != null) {
builderCall.callback(bulkBuilder);
}
final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
final BulkItemResponse[] itemResponses = response.getItems();
if (itemResponses.length != entityList.size()) {
throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
}
final int[] results = new int[itemResponses.length];
for (int i = 0; i < itemResponses.length; i++) {
final BulkItemResponse itemResponse = itemResponses[i];
final Entity entity = entityList.get(i);
if (entity instanceof EsAbstractEntity) {
((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
}
results[i] = itemResponse.isFailed() ? 0 : 1;
}
return results;
}
Aggregations