use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchBulkDocumentWriter method handleBulkResponse.
/**
* Handles the {@link BulkResponse} received from Elasticsearch.
* @param bulkResponse The response received from Elasticsearch.
* @param documents The documents included in the bulk request.
* @param results The writer results.
*/
private void handleBulkResponse(BulkResponse bulkResponse, List<Indexable> documents, BulkDocumentWriterResults<D> results) {
if (bulkResponse.hasFailures()) {
// interrogate the response to distinguish between those that succeeded and those that failed
for (BulkItemResponse response : bulkResponse) {
if (response.isFailed()) {
// request failed
D failed = getDocument(response.getItemId());
Exception cause = response.getFailure().getCause();
String message = response.getFailureMessage();
results.addFailure(failed, cause, message);
} else {
// request succeeded
D success = getDocument(response.getItemId());
success.setDocumentID(response.getResponse().getId());
results.addSuccess(success);
}
}
} else {
// all requests succeeded
for (Indexable success : documents) {
results.addSuccess(success.document);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project metron by apache.
the class ElasticsearchBulkDocumentWriterTest method setupElasticsearchToFail.
private void setupElasticsearchToFail() throws IOException {
final String errorMessage = "error message";
final Exception cause = new Exception("test exception");
final boolean isFailed = true;
final int itemID = 0;
// define the item failure
BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
when(failure.getCause()).thenReturn(cause);
when(failure.getMessage()).thenReturn(errorMessage);
// define the item level response
BulkItemResponse itemResponse = mock(BulkItemResponse.class);
when(itemResponse.isFailed()).thenReturn(isFailed);
when(itemResponse.getItemId()).thenReturn(itemID);
when(itemResponse.getFailure()).thenReturn(failure);
when(itemResponse.getFailureMessage()).thenReturn("error message");
List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);
// define the bulk response to indicate failure
BulkResponse response = mock(BulkResponse.class);
when(response.iterator()).thenReturn(itemsResponses.iterator());
when(response.hasFailures()).thenReturn(isFailed);
// have the client return the mock response
when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project elasticsearch by elastic.
the class ShardInfoIT method testBulkWithIndexAndDeleteItems.
public void testBulkWithIndexAndDeleteItems() throws Exception {
prepareIndex(1);
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
for (int i = 0; i < 10; i++) {
bulkRequestBuilder.add(client().prepareIndex("idx", "type").setSource("{}", XContentType.JSON));
}
BulkResponse bulkResponse = bulkRequestBuilder.get();
bulkRequestBuilder = client().prepareBulk();
for (BulkItemResponse item : bulkResponse) {
assertThat(item.isFailed(), equalTo(false));
assertShardInfo(item.getResponse());
bulkRequestBuilder.add(client().prepareDelete("idx", "type", item.getId()));
}
bulkResponse = bulkRequestBuilder.get();
for (BulkItemResponse item : bulkResponse) {
assertThat(item.isFailed(), equalTo(false));
assertShardInfo(item.getResponse());
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project elasticsearch by elastic.
the class ShardInfoIT method testBulkWithUpdateItems.
public void testBulkWithUpdateItems() throws Exception {
prepareIndex(1);
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
for (int i = 0; i < 10; i++) {
bulkRequestBuilder.add(client().prepareUpdate("idx", "type", Integer.toString(i)).setDoc("{}", XContentType.JSON).setDocAsUpsert(true));
}
BulkResponse bulkResponse = bulkRequestBuilder.get();
for (BulkItemResponse item : bulkResponse) {
assertThat(item.isFailed(), equalTo(false));
assertShardInfo(item.getResponse());
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkItemResponse in project elasticsearch by elastic.
the class GeoFilterIT method testBulk.
public void testBulk() throws Exception {
byte[] bulkAction = unZipData("/org/elasticsearch/search/geo/gzippedmap.gz");
Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("country").startObject("properties").startObject("pin").field("type", "geo_point");
xContentBuilder.field("store", true).endObject().startObject("location").field("type", "geo_shape").endObject().endObject().endObject().endObject();
client().admin().indices().prepareCreate("countries").setSettings(settings).addMapping("country", xContentBuilder).execute().actionGet();
BulkResponse bulk = client().prepareBulk().add(bulkAction, 0, bulkAction.length, null, null, xContentBuilder.contentType()).get();
for (BulkItemResponse item : bulk.getItems()) {
assertFalse("unable to index data", item.isFailed());
}
client().admin().indices().prepareRefresh().execute().actionGet();
String key = "DE";
SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("_id", key)).execute().actionGet();
assertHitCount(searchResponse, 1);
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), equalTo(key));
}
SearchResponse world = client().prepareSearch().addStoredField("pin").setQuery(geoBoundingBoxQuery("pin").setCorners(90, -179.99999, -90, 179.99999)).execute().actionGet();
assertHitCount(world, 53);
SearchResponse distance = client().prepareSearch().addStoredField("pin").setQuery(geoDistanceQuery("pin").distance("425km").point(51.11, 9.851)).execute().actionGet();
assertHitCount(distance, 5);
GeoPoint point = new GeoPoint();
for (SearchHit hit : distance.getHits()) {
String name = hit.getId();
point.resetFromString(hit.getFields().get("pin").getValue());
double dist = distance(point.getLat(), point.getLon(), 51.11, 9.851);
assertThat("distance to '" + name + "'", dist, lessThanOrEqualTo(425000d));
assertThat(name, anyOf(equalTo("CZ"), equalTo("DE"), equalTo("BE"), equalTo("NL"), equalTo("LU")));
if (key.equals(name)) {
assertThat(dist, closeTo(0d, 0.1d));
}
}
}
Aggregations