Search in sources :

Example 26 with BulkItemResponse

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);
        }
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) IOException(java.io.IOException)

Example 27 with BulkItemResponse

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);
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException)

Example 28 with BulkItemResponse

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());
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 29 with BulkItemResponse

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());
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Example 30 with BulkItemResponse

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));
        }
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchHit(org.elasticsearch.search.SearchHit) Version(org.elasticsearch.Version) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)48 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)37 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)16 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)14 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)7 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)7 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)7 NodeClosedException (org.elasticsearch.node.NodeClosedException)7 ReceiveTimeoutTransportException (org.elasticsearch.transport.ReceiveTimeoutTransportException)7 List (java.util.List)6 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 FailedNodeException (org.elasticsearch.action.FailedNodeException)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5 TransportException (org.elasticsearch.transport.TransportException)5 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)4