Search in sources :

Example 61 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project tmail-backend by linagora.

the class ElasticSearchIndexer method update.

public Mono<BulkResponse> update(List<UpdatedRepresentation> updatedDocumentParts, RoutingKey routingKey) {
    Preconditions.checkNotNull(updatedDocumentParts);
    Preconditions.checkNotNull(routingKey);
    BulkRequest request = new BulkRequest();
    updatedDocumentParts.forEach(updatedDocumentPart -> request.add(new UpdateRequest(aliasName.getValue(), NodeMappingFactory.DEFAULT_MAPPING_NAME, updatedDocumentPart.getId().asString()).doc(updatedDocumentPart.getUpdatedDocumentPart(), XContentType.JSON).routing(routingKey.asString())));
    return client.bulk(request, RequestOptions.DEFAULT).onErrorResume(ValidationException.class, exception -> {
        LOGGER.warn("Error while updating index", exception);
        return Mono.empty();
    });
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest)

Example 62 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project flink by splunk.

the class Elasticsearch6SinkBuilder method getBulkProcessorBuilderFactory.

@Override
protected BulkProcessorBuilderFactory getBulkProcessorBuilderFactory() {
    return new BulkProcessorBuilderFactory() {

        @Override
        public BulkProcessor.Builder apply(RestHighLevelClient client, BulkProcessorConfig bulkProcessorConfig, BulkProcessor.Listener listener) {
            BulkProcessor.Builder builder = BulkProcessor.builder(new // This cannot be inlined as a
            BulkRequestConsumerFactory() {

                // lambda because then
                // deserialization fails
                @Override
                public void accept(BulkRequest bulkRequest, ActionListener<BulkResponse> bulkResponseActionListener) {
                    client.bulkAsync(bulkRequest, bulkResponseActionListener);
                }
            }, listener);
            if (bulkProcessorConfig.getBulkFlushMaxActions() != -1) {
                builder.setBulkActions(bulkProcessorConfig.getBulkFlushMaxActions());
            }
            if (bulkProcessorConfig.getBulkFlushMaxMb() != -1) {
                builder.setBulkSize(new ByteSizeValue(bulkProcessorConfig.getBulkFlushMaxMb(), ByteSizeUnit.MB));
            }
            if (bulkProcessorConfig.getBulkFlushInterval() != -1) {
                builder.setFlushInterval(new TimeValue(bulkProcessorConfig.getBulkFlushInterval()));
            }
            BackoffPolicy backoffPolicy;
            final TimeValue backoffDelay = new TimeValue(bulkProcessorConfig.getBulkFlushBackOffDelay());
            final int maxRetryCount = bulkProcessorConfig.getBulkFlushBackoffRetries();
            switch(bulkProcessorConfig.getFlushBackoffType()) {
                case CONSTANT:
                    backoffPolicy = BackoffPolicy.constantBackoff(backoffDelay, maxRetryCount);
                    break;
                case EXPONENTIAL:
                    backoffPolicy = BackoffPolicy.exponentialBackoff(backoffDelay, maxRetryCount);
                    break;
                case NONE:
                    backoffPolicy = BackoffPolicy.noBackoff();
                    break;
                default:
                    throw new IllegalArgumentException("Received unknown backoff policy type " + bulkProcessorConfig.getFlushBackoffType());
            }
            builder.setBackoffPolicy(backoffPolicy);
            return builder;
        }
    };
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) BulkProcessor(org.elasticsearch.action.bulk.BulkProcessor) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 63 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project flink by splunk.

the class Elasticsearch7SinkBuilder method getBulkProcessorBuilderFactory.

@Override
protected BulkProcessorBuilderFactory getBulkProcessorBuilderFactory() {
    return new BulkProcessorBuilderFactory() {

        @Override
        public BulkProcessor.Builder apply(RestHighLevelClient client, BulkProcessorConfig bulkProcessorConfig, BulkProcessor.Listener listener) {
            BulkProcessor.Builder builder = BulkProcessor.builder(new // This cannot be inlined as a
            BulkRequestConsumerFactory() {

                // lambda because then
                // deserialization fails
                @Override
                public void accept(BulkRequest bulkRequest, ActionListener<BulkResponse> bulkResponseActionListener) {
                    client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, bulkResponseActionListener);
                }
            }, listener);
            if (bulkProcessorConfig.getBulkFlushMaxActions() != -1) {
                builder.setBulkActions(bulkProcessorConfig.getBulkFlushMaxActions());
            }
            if (bulkProcessorConfig.getBulkFlushMaxMb() != -1) {
                builder.setBulkSize(new ByteSizeValue(bulkProcessorConfig.getBulkFlushMaxMb(), ByteSizeUnit.MB));
            }
            if (bulkProcessorConfig.getBulkFlushInterval() != -1) {
                builder.setFlushInterval(new TimeValue(bulkProcessorConfig.getBulkFlushInterval()));
            }
            BackoffPolicy backoffPolicy;
            final TimeValue backoffDelay = new TimeValue(bulkProcessorConfig.getBulkFlushBackOffDelay());
            final int maxRetryCount = bulkProcessorConfig.getBulkFlushBackoffRetries();
            switch(bulkProcessorConfig.getFlushBackoffType()) {
                case CONSTANT:
                    backoffPolicy = BackoffPolicy.constantBackoff(backoffDelay, maxRetryCount);
                    break;
                case EXPONENTIAL:
                    backoffPolicy = BackoffPolicy.exponentialBackoff(backoffDelay, maxRetryCount);
                    break;
                case NONE:
                    backoffPolicy = BackoffPolicy.noBackoff();
                    break;
                default:
                    throw new IllegalArgumentException("Received unknown backoff policy type " + bulkProcessorConfig.getFlushBackoffType());
            }
            builder.setBackoffPolicy(backoffPolicy);
            return builder;
        }
    };
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) BulkProcessor(org.elasticsearch.action.bulk.BulkProcessor) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 64 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project open-commerce-search by CommerceExperts.

the class ElasticsearchIndexClient method deleteDocuments.

public List<DeleteResponse> deleteDocuments(String index, List<String> ids) throws IOException {
    BulkRequest bulkRequest = new BulkRequest(index);
    for (String id : ids) {
        bulkRequest.add(new DeleteRequest().id(id));
    }
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    List<DeleteResponse> responses = new ArrayList<>();
    for (BulkItemResponse item : bulkResponse.getItems()) {
        responses.add((DeleteResponse) item.getResponse());
    }
    return responses;
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ArrayList(java.util.ArrayList) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 65 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project open-commerce-search by CommerceExperts.

the class ElasticsearchIndexClient method indexRecordsChunkwise.

/**
 * Will split (if necessary) the given records into several bulk requests
 * each with the specified maximum size. The target index will be delete
 * before if already present.
 *
 * @param records
 * @param maxBulkSize
 * @return
 * @throws IOException
 */
public List<BulkResponse> indexRecordsChunkwise(String indexName, Iterator<IndexableItem> records, int maxBulkSize) throws IOException {
    List<BulkResponse> responses = new ArrayList<>();
    BulkRequest bulkIndexRequest = new BulkRequest();
    int i = 0;
    int indexedTotal = 0;
    while (records.hasNext()) {
        IndexRequest indexRequest;
        try {
            IndexableItem nextRecord = records.next();
            if (nextRecord != null) {
                indexRequest = asIndexRequest(indexName, nextRecord);
                bulkIndexRequest.add(indexRequest);
                i++;
                if (i == maxBulkSize) {
                    indexedTotal += i;
                    i = 0;
                    responses.add(highLevelClient.bulk(bulkIndexRequest, RequestOptions.DEFAULT));
                    log.info("Indexed {} records", indexedTotal);
                    bulkIndexRequest = new BulkRequest();
                }
            }
        } catch (JsonProcessingException e) {
            log.warn("failed to add record to bulk request", e);
        }
    }
    if (i > 0) {
        responses.add(highLevelClient.bulk(bulkIndexRequest, RequestOptions.DEFAULT));
    }
    return responses;
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ArrayList(java.util.ArrayList) IndexableItem(de.cxp.ocs.indexer.model.IndexableItem) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

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