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;
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchWriter method buildWriteReponse.
protected BulkWriterResponse buildWriteReponse(Iterable<Tuple> tuples, BulkResponse bulkResponse) throws Exception {
// Elasticsearch responses are in the same order as the request, giving us an implicit mapping with Tuples
BulkWriterResponse writerResponse = new BulkWriterResponse();
if (bulkResponse.hasFailures()) {
Iterator<BulkItemResponse> respIter = bulkResponse.iterator();
Iterator<Tuple> tupleIter = tuples.iterator();
while (respIter.hasNext() && tupleIter.hasNext()) {
BulkItemResponse item = respIter.next();
Tuple tuple = tupleIter.next();
if (item.isFailed()) {
writerResponse.addError(item.getFailure().getCause(), tuple);
} else {
writerResponse.addSuccess(tuple);
}
// Should never happen, so fail the entire batch if it does.
if (respIter.hasNext() != tupleIter.hasNext()) {
throw new Exception(bulkResponse.buildFailureMessage());
}
}
} else {
writerResponse.addAllSuccesses(tuples);
}
return writerResponse;
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchWriterTest method testSuccessAndFailure.
@Test
public void testSuccessAndFailure() throws Exception {
Tuple tuple1 = mock(Tuple.class);
Tuple tuple2 = mock(Tuple.class);
BulkResponse response = mock(BulkResponse.class);
when(response.hasFailures()).thenReturn(true);
Exception e = new IllegalStateException("Cause");
BulkItemResponse itemResponse = buildBulkItemFailure(e);
BulkItemResponse itemResponse2 = mock(BulkItemResponse.class);
when(itemResponse2.isFailed()).thenReturn(false);
when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
BulkWriterResponse expected = new BulkWriterResponse();
expected.addError(e, tuple1);
expected.addSuccess(tuple2);
ElasticsearchWriter esWriter = new ElasticsearchWriter();
BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
assertEquals("Response should have one error and one success", expected, actual);
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchWriterTest method testTwoSameFailure.
@Test
public void testTwoSameFailure() throws Exception {
Tuple tuple1 = mock(Tuple.class);
Tuple tuple2 = mock(Tuple.class);
BulkResponse response = mock(BulkResponse.class);
when(response.hasFailures()).thenReturn(true);
Exception e = new IllegalStateException();
BulkItemResponse itemResponse = buildBulkItemFailure(e);
BulkItemResponse itemResponse2 = buildBulkItemFailure(e);
when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
BulkWriterResponse expected = new BulkWriterResponse();
expected.addError(e, tuple1);
expected.addError(e, tuple2);
ElasticsearchWriter esWriter = new ElasticsearchWriter();
BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
assertEquals("Response should have two errors and no successes", expected, actual);
// Ensure the errors actually get collapsed together
Map<Throwable, Collection<Tuple>> actualErrors = actual.getErrors();
HashMap<Throwable, Collection<Tuple>> expectedErrors = new HashMap<>();
expectedErrors.put(e, ImmutableList.of(tuple1, tuple2));
assertEquals("Errors should have collapsed together", expectedErrors, actualErrors);
}
use of org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchWriterTest method testTwoDifferentFailure.
@Test
public void testTwoDifferentFailure() throws Exception {
Tuple tuple1 = mock(Tuple.class);
Tuple tuple2 = mock(Tuple.class);
BulkResponse response = mock(BulkResponse.class);
when(response.hasFailures()).thenReturn(true);
Exception e = new IllegalStateException("Cause");
Exception e2 = new IllegalStateException("Different Cause");
BulkItemResponse itemResponse = buildBulkItemFailure(e);
BulkItemResponse itemResponse2 = buildBulkItemFailure(e2);
when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
BulkWriterResponse expected = new BulkWriterResponse();
expected.addError(e, tuple1);
expected.addError(e2, tuple2);
ElasticsearchWriter esWriter = new ElasticsearchWriter();
BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
assertEquals("Response should have two errors and no successes", expected, actual);
// Ensure the errors did not get collapsed together
Map<Throwable, Collection<Tuple>> actualErrors = actual.getErrors();
HashMap<Throwable, Collection<Tuple>> expectedErrors = new HashMap<>();
expectedErrors.put(e, ImmutableList.of(tuple1));
expectedErrors.put(e2, ImmutableList.of(tuple2));
assertEquals("Errors should not have collapsed together", expectedErrors, actualErrors);
}
Aggregations