Search in sources :

Example 21 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project camel by apache.

the class ElasticsearchProducer method process.

public void process(Exchange exchange) throws Exception {
    // 2. Index and type will be set by:
    // a. If the incoming body is already an action request
    // b. If the body is not an action request we will use headers if they
    // are set.
    // c. If the body is not an action request and the headers aren't set we
    // will use the configuration.
    // No error is thrown by the component in the event none of the above
    // conditions are met. The java es client
    // will throw.
    Message message = exchange.getIn();
    final ElasticsearchOperation operation = resolveOperation(exchange);
    // Set the index/type headers on the exchange if necessary. This is used
    // for type conversion.
    boolean configIndexName = false;
    String indexName = message.getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class);
    if (indexName == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_NAME, configuration.getIndexName());
        configIndexName = true;
    }
    boolean configIndexType = false;
    String indexType = message.getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class);
    if (indexType == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, configuration.getIndexType());
        configIndexType = true;
    }
    boolean configWaitForActiveShards = false;
    Integer waitForActiveShards = message.getHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, Integer.class);
    if (waitForActiveShards == null) {
        message.setHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS, configuration.getWaitForActiveShards());
        configWaitForActiveShards = true;
    }
    if (operation == ElasticsearchOperation.INDEX) {
        IndexRequest indexRequest = message.getBody(IndexRequest.class);
        message.setBody(client.index(indexRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.UPDATE) {
        UpdateRequest updateRequest = message.getBody(UpdateRequest.class);
        message.setBody(client.update(updateRequest).actionGet().getId());
    } else if (operation == ElasticsearchOperation.GET_BY_ID) {
        GetRequest getRequest = message.getBody(GetRequest.class);
        message.setBody(client.get(getRequest));
    } else if (operation == ElasticsearchOperation.MULTIGET) {
        MultiGetRequest multiGetRequest = message.getBody(MultiGetRequest.class);
        message.setBody(client.multiGet(multiGetRequest));
    } else if (operation == ElasticsearchOperation.BULK) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        message.setBody(client.bulk(bulkRequest).actionGet());
    } else if (operation == ElasticsearchOperation.BULK_INDEX) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        List<String> indexedIds = new ArrayList<String>();
        for (BulkItemResponse response : client.bulk(bulkRequest).actionGet().getItems()) {
            indexedIds.add(response.getId());
        }
        message.setBody(indexedIds);
    } else if (operation == ElasticsearchOperation.DELETE) {
        DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
        message.setBody(client.delete(deleteRequest).actionGet());
    } else if (operation == ElasticsearchOperation.EXISTS) {
        // ExistsRequest API is deprecated, using SearchRequest instead with size=0 and terminate_after=1
        SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class));
        try {
            client.prepareSearch(searchRequest.indices()).setSize(0).setTerminateAfter(1).get();
            message.setBody(true);
        } catch (IndexNotFoundException e) {
            message.setBody(false);
        }
    } else if (operation == ElasticsearchOperation.SEARCH) {
        SearchRequest searchRequest = message.getBody(SearchRequest.class);
        message.setBody(client.search(searchRequest).actionGet());
    } else if (operation == ElasticsearchOperation.MULTISEARCH) {
        MultiSearchRequest multiSearchRequest = message.getBody(MultiSearchRequest.class);
        message.setBody(client.multiSearch(multiSearchRequest));
    } else if (operation == ElasticsearchOperation.DELETE_INDEX) {
        DeleteIndexRequest deleteIndexRequest = message.getBody(DeleteIndexRequest.class);
        message.setBody(client.admin().indices().delete(deleteIndexRequest).actionGet());
    } else {
        throw new IllegalArgumentException(ElasticsearchConstants.PARAM_OPERATION + " value '" + operation + "' is not supported");
    }
    // subsequent endpoint index/type with the first endpoint index/type.
    if (configIndexName) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_NAME);
    }
    if (configIndexType) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_TYPE);
    }
    if (configWaitForActiveShards) {
        message.removeHeader(ElasticsearchConstants.PARAM_WAIT_FOR_ACTIVE_SHARDS);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Message(org.apache.camel.Message) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 22 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project camel by apache.

the class ElasticsearchProducer method process.

public void process(Exchange exchange) throws Exception {
    // 2. Index and type will be set by:
    // a. If the incoming body is already an action request
    // b. If the body is not an action request we will use headers if they
    // are set.
    // c. If the body is not an action request and the headers aren't set we
    // will use the configuration.
    // No error is thrown by the component in the event none of the above
    // conditions are met. The java es client
    // will throw.
    Message message = exchange.getIn();
    final String operation = resolveOperation(exchange);
    // Set the index/type headers on the exchange if necessary. This is used
    // for type conversion.
    boolean configIndexName = false;
    String indexName = message.getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class);
    if (indexName == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_NAME, getEndpoint().getConfig().getIndexName());
        configIndexName = true;
    }
    boolean configIndexType = false;
    String indexType = message.getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class);
    if (indexType == null) {
        message.setHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, getEndpoint().getConfig().getIndexType());
        configIndexType = true;
    }
    boolean configConsistencyLevel = false;
    String consistencyLevel = message.getHeader(ElasticsearchConstants.PARAM_CONSISTENCY_LEVEL, String.class);
    if (consistencyLevel == null) {
        message.setHeader(ElasticsearchConstants.PARAM_CONSISTENCY_LEVEL, getEndpoint().getConfig().getConsistencyLevel());
        configConsistencyLevel = true;
    }
    Client client = getEndpoint().getClient();
    if (ElasticsearchConstants.OPERATION_INDEX.equals(operation)) {
        IndexRequest indexRequest = message.getBody(IndexRequest.class);
        message.setBody(client.index(indexRequest).actionGet().getId());
    } else if (ElasticsearchConstants.OPERATION_UPDATE.equals(operation)) {
        UpdateRequest updateRequest = message.getBody(UpdateRequest.class);
        message.setBody(client.update(updateRequest).actionGet().getId());
    } else if (ElasticsearchConstants.OPERATION_GET_BY_ID.equals(operation)) {
        GetRequest getRequest = message.getBody(GetRequest.class);
        message.setBody(client.get(getRequest));
    } else if (ElasticsearchConstants.OPERATION_MULTIGET.equals(operation)) {
        MultiGetRequest multiGetRequest = message.getBody(MultiGetRequest.class);
        message.setBody(client.multiGet(multiGetRequest));
    } else if (ElasticsearchConstants.OPERATION_BULK.equals(operation)) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        message.setBody(client.bulk(bulkRequest).actionGet());
    } else if (ElasticsearchConstants.OPERATION_BULK_INDEX.equals(operation)) {
        BulkRequest bulkRequest = message.getBody(BulkRequest.class);
        List<String> indexedIds = new ArrayList<String>();
        for (BulkItemResponse response : client.bulk(bulkRequest).actionGet().getItems()) {
            indexedIds.add(response.getId());
        }
        message.setBody(indexedIds);
    } else if (ElasticsearchConstants.OPERATION_DELETE.equals(operation)) {
        DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
        message.setBody(client.delete(deleteRequest).actionGet());
    } else if (ElasticsearchConstants.OPERATION_EXISTS.equals(operation)) {
        ExistsRequest existsRequest = message.getBody(ExistsRequest.class);
        message.setBody(client.admin().indices().prepareExists(existsRequest.indices()).get().isExists());
    } else if (ElasticsearchConstants.OPERATION_SEARCH.equals(operation)) {
        SearchRequest searchRequest = message.getBody(SearchRequest.class);
        message.setBody(client.search(searchRequest).actionGet());
    } else if (ElasticsearchConstants.OPERATION_MULTISEARCH.equals(operation)) {
        MultiSearchRequest multiSearchRequest = message.getBody(MultiSearchRequest.class);
        message.setBody(client.multiSearch(multiSearchRequest));
    } else if (ElasticsearchConstants.OPERATION_DELETE_INDEX.equals(operation)) {
        DeleteIndexRequest deleteIndexRequest = message.getBody(DeleteIndexRequest.class);
        message.setBody(client.admin().indices().delete(deleteIndexRequest).actionGet());
    } else {
        throw new IllegalArgumentException(ElasticsearchConstants.PARAM_OPERATION + " value '" + operation + "' is not supported");
    }
    // subsequent endpoint index/type with the first endpoint index/type.
    if (configIndexName) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_NAME);
    }
    if (configIndexType) {
        message.removeHeader(ElasticsearchConstants.PARAM_INDEX_TYPE);
    }
    if (configConsistencyLevel) {
        message.removeHeader(ElasticsearchConstants.PARAM_CONSISTENCY_LEVEL);
    }
}
Also used : ExistsRequest(org.elasticsearch.action.exists.ExistsRequest) SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) Message(org.apache.camel.Message) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) ArrayList(java.util.ArrayList) List(java.util.List) Client(org.elasticsearch.client.Client) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 23 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project titan by thinkaurelius.

the class ElasticSearchIndex method mutate.

@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    BulkRequestBuilder brb = client.prepareBulk();
    int bulkrequests = 0;
    try {
        for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
            String storename = stores.getKey();
            for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
                String docid = entry.getKey();
                IndexMutation mutation = entry.getValue();
                assert mutation.isConsolidated();
                Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
                Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
                Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
                //Deletions first
                if (mutation.hasDeletions()) {
                    if (mutation.isDeleted()) {
                        log.trace("Deleting entire document {}", docid);
                        brb.add(new DeleteRequest(indexName, storename, docid));
                    } else {
                        String script = getDeletionScript(informations, storename, mutation);
                        brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE));
                        log.trace("Adding script {}", script);
                    }
                    bulkrequests++;
                }
                if (mutation.hasAdditions()) {
                    int ttl = mutation.determineTTL();
                    if (mutation.isNew()) {
                        //Index
                        log.trace("Adding entire document {}", docid);
                        brb.add(new IndexRequest(indexName, storename, docid).source(getNewDocument(mutation.getAdditions(), informations.get(storename), ttl)));
                    } else {
                        Preconditions.checkArgument(ttl == 0, "Elasticsearch only supports TTL on new documents [%s]", docid);
                        boolean needUpsert = !mutation.hasDeletions();
                        String script = getAdditionScript(informations, storename, mutation);
                        UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE);
                        if (needUpsert) {
                            XContentBuilder doc = getNewDocument(mutation.getAdditions(), informations.get(storename), ttl);
                            update.setUpsert(doc);
                        }
                        brb.add(update);
                        log.trace("Adding script {}", script);
                    }
                    bulkrequests++;
                }
            }
        }
        if (bulkrequests > 0) {
            BulkResponse bulkItemResponses = brb.execute().actionGet();
            if (bulkItemResponses.hasFailures()) {
                boolean actualFailure = false;
                for (BulkItemResponse response : bulkItemResponses.getItems()) {
                    //The document may have been deleted, which is OK
                    if (response.isFailed() && response.getFailure().getStatus() != RestStatus.NOT_FOUND) {
                        log.error("Failed to execute ES query {}", response.getFailureMessage());
                        actualFailure = true;
                    }
                }
                if (actualFailure) {
                    throw new Exception(bulkItemResponses.buildFailureMessage());
                }
            }
        }
    } catch (Exception e) {
        log.error("Failed to execute ES query {}", brb.request().timeout(), e);
        throw convert(e);
    }
}
Also used : UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) FileNotFoundException(java.io.FileNotFoundException) TitanException(com.thinkaurelius.titan.core.TitanException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) IOException(java.io.IOException) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 24 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project metacat by Netflix.

the class ElasticSearchUtilImpl method bulkSaveToIndex.

/**
     * Bulk save of the entities.
     *
     * @param type index type
     * @param docs metacat documents
     */
void bulkSaveToIndex(final String type, final List<ElasticSearchDoc> docs, final String index) {
    if (docs != null && !docs.isEmpty()) {
        try {
            RETRY_ES_PUBLISH.call(() -> {
                final BulkRequestBuilder bulkRequest = client.prepareBulk();
                docs.forEach(doc -> bulkRequest.add(client.prepareIndex(index, type, doc.getId()).setSource(doc.toJsonString())));
                if (bulkRequest.numberOfActions() > 0) {
                    final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                    log.info("Bulk saving metadata of index {} type {} with size {}.", index, type, docs.size());
                    if (bulkResponse.hasFailures()) {
                        for (BulkItemResponse item : bulkResponse.getItems()) {
                            if (item.isFailed()) {
                                log.error("Failed saving metadata of {} index type {} with id {}. Message: {}", index, type, item.getId(), item.getFailureMessage());
                                registry.counter(registry.createId(Metrics.CounterElasticSearchSave.name()).withTags(Metrics.statusFailureMap)).increment();
                                log("ElasticSearchUtil.bulkSaveToIndex.index", type, item.getId(), null, item.getFailureMessage(), null, true, index);
                            }
                        }
                    }
                }
                return null;
            });
        } catch (Exception e) {
            log.error(String.format("Failed saving metadatas of index %s type %s", index, type), e);
            registry.counter(registry.createId(Metrics.CounterElasticSearchBulkSave.name()).withTags(Metrics.statusFailureMap)).increment();
            final List<String> docIds = docs.stream().map(ElasticSearchDoc::getId).collect(Collectors.toList());
            log("ElasticSearchUtil.bulkSaveToIndex", type, docIds.toString(), null, e.getMessage(), e, true, index);
        }
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) List(java.util.List) ArrayList(java.util.ArrayList) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FailedNodeException(org.elasticsearch.action.FailedNodeException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 25 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project metacat by Netflix.

the class ElasticSearchUtilImpl method updateDocs.

/**
     * Updates the documents with partial updates with the given fields.
     *
     * @param type                  index type
     * @param ids                   list of entity ids
     * @param metacatRequestContext context containing the user name
     * @param node                  json that represents the document source
     */
private void updateDocs(final String type, final List<String> ids, final MetacatRequestContext metacatRequestContext, final ObjectNode node) {
    try {
        RETRY_ES_PUBLISH.call(() -> {
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            ids.forEach(id -> {
                node.put(ElasticSearchDoc.Field.USER, metacatRequestContext.getUserName());
                bulkRequest.add(client.prepareUpdate(esIndex, type, id).setRetryOnConflict(NO_OF_CONFLICT_RETRIES).setDoc(metacatJson.toJsonAsBytes(node)));
            });
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
            if (bulkResponse.hasFailures()) {
                for (BulkItemResponse item : bulkResponse.getItems()) {
                    if (item.isFailed()) {
                        log.error("Failed updating metadata of type {} with id {}. Message: {}", type, item.getId(), item.getFailureMessage());
                        registry.counter(registry.createId(Metrics.CounterElasticSearchUpdate.name()).withTags(Metrics.statusFailureMap)).increment();
                        log("ElasticSearchUtil.updateDocs.item", type, item.getId(), null, item.getFailureMessage(), null, true);
                    }
                }
            }
            return null;
        });
    } catch (Exception e) {
        log.error(String.format("Failed updating metadata of type %s with ids %s", type, ids), e);
        registry.counter(registry.createId(Metrics.CounterElasticSearchBulkUpdate.name()).withTags(Metrics.statusFailureMap)).increment();
        log("ElasticSearchUtil.updatDocs", type, ids.toString(), null, e.getMessage(), e, true);
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FailedNodeException(org.elasticsearch.action.FailedNodeException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)29 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)24 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)13 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)6 ArrayList (java.util.ArrayList)5 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)5 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 FailedNodeException (org.elasticsearch.action.FailedNodeException)4 Failure (org.elasticsearch.action.bulk.BulkItemResponse.Failure)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)4 IOException (java.io.IOException)3 List (java.util.List)3 Entity (org.dbflute.Entity)3 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)3 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)3 SearchFailure (org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.SearchFailure)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)3