Search in sources :

Example 56 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project vind by RBMHTechnology.

the class BulkRequestBuilder method executeBulk.

public static BulkResponse executeBulk(BulkRequest bulkRequest, RequestOptions options, String indexName, RestHighLevelClient client) throws IOException {
    ActionRequestValidationException validationException = bulkRequest.validate();
    if (validationException != null && validationException.validationErrors().isEmpty() == false) {
        throw validationException;
    }
    Request req = toRequest(bulkRequest, indexName);
    req.setOptions(options);
    Response response;
    try {
        response = client.getLowLevelClient().performRequest(req);
    } catch (ResponseException e) {
        throw new IOException("Indexing was not successful", e);
    }
    try {
        return client.parseEntity(response.getEntity(), BulkResponse::fromXContent);
    } catch (Exception e) {
        throw new IOException("Unable to parse response body for " + response, e);
    }
}
Also used : BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) IOException(java.io.IOException)

Example 57 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project vind by RBMHTechnology.

the class ElasticVindClient method add.

public BulkResponse add(Map<String, Object> jsonDoc) throws IOException {
    final BulkRequest bulkIndexRequest = new BulkRequest(defaultIndex);
    bulkIndexRequest.add(ElasticRequestUtils.getIndexRequest(jsonDoc));
    bulkIndexRequest.timeout(TimeValue.timeValueMillis(connectionTimeout));
    bulkIndexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    return BulkRequestBuilder.executeBulk(bulkIndexRequest, RequestOptions.DEFAULT, defaultIndex, client);
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Example 58 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project phoebus-olog by Olog.

the class LogbookRepository method saveAll.

@Override
public <S extends Logbook> Iterable<S> saveAll(Iterable<S> logbooks) {
    BulkRequest bulk = new BulkRequest();
    logbooks.forEach(logbook -> {
        try {
            bulk.add(new IndexRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, logbook.getName()).source(mapper.writeValueAsBytes(logbook), XContentType.JSON));
        } catch (JsonProcessingException e) {
            logger.log(Level.SEVERE, "Failed to create logbook: " + logbook, e);
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create logbook: " + logbook);
        }
    });
    bulk.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    BulkResponse bulkResponse;
    try {
        bulkResponse = client.bulk(bulk, RequestOptions.DEFAULT);
        if (bulkResponse.hasFailures()) {
            // process failures by iterating through each bulk response item
            bulkResponse.forEach(response -> {
                if (response.getFailure() != null) {
                    logger.log(Level.SEVERE, response.getFailureMessage(), response.getFailure().getCause());
                }
            });
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create logbooks: " + logbooks);
        } else {
            return logbooks;
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to create logbooks: " + logbooks, e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create logbooks: " + logbooks);
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) IndexRequest(org.elasticsearch.action.index.IndexRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResponseStatusException(org.springframework.web.server.ResponseStatusException)

Example 59 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project phoebus-olog by Olog.

the class PropertyRepositoryIT method cleanupProperties.

/**
 * Clean up the properties
 *
 * @param properties
 */
private void cleanupProperties(List<Property> properties) {
    try {
        BulkRequest bulk = new BulkRequest();
        properties.forEach(property -> {
            bulk.add(new DeleteRequest(ES_PROPERTY_INDEX, ES_PROPERTY_TYPE, property.getName()));
        });
        client.bulk(bulk, RequestOptions.DEFAULT);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IOException(java.io.IOException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 60 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project phoebus-olog by Olog.

the class LogbookRepositoryIT method cleanupLogbook.

/**
 * Cleanup up the logbooks
 *
 * @param logbooks
 * @throws IOException
 */
void cleanupLogbook(List<Logbook> logbooks) throws IOException {
    // Manual cleanup since Olog does not delete things
    BulkRequest bulk = new BulkRequest();
    logbooks.forEach(logbook -> {
        bulk.add(new DeleteRequest(ES_LOGBOOK_INDEX, ES_LOGBOOK_TYPE, logbook.getName()));
    });
    client.bulk(bulk, RequestOptions.DEFAULT);
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

BulkRequest (org.elasticsearch.action.bulk.BulkRequest)158 IndexRequest (org.elasticsearch.action.index.IndexRequest)77 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)73 IOException (java.io.IOException)47 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)40 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)28 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)27 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)25 ArrayList (java.util.ArrayList)24 List (java.util.List)18 SearchRequest (org.elasticsearch.action.search.SearchRequest)17 Test (org.junit.jupiter.api.Test)17 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)16 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)16 Map (java.util.Map)15 Test (org.junit.Test)15 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 BulkProcessor (org.elasticsearch.action.bulk.BulkProcessor)13 ActionListener (org.elasticsearch.action.ActionListener)12