use of org.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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.graylog.shaded.elasticsearch7.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