Search in sources :

Example 36 with BulkRequest

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

the class DynamicMappingDisabledTests method testDynamicDisabled.

public void testDynamicDisabled() {
    IndexRequest request = new IndexRequest("index", "type", "1");
    request.source(Requests.INDEX_CONTENT_TYPE, "foo", 3);
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(request);
    final AtomicBoolean onFailureCalled = new AtomicBoolean();
    transportBulkAction.execute(bulkRequest, new ActionListener<BulkResponse>() {

        @Override
        public void onResponse(BulkResponse bulkResponse) {
            fail("onResponse shouldn't be called");
        }

        @Override
        public void onFailure(Exception e) {
            onFailureCalled.set(true);
            assertThat(e, instanceOf(IndexNotFoundException.class));
            assertEquals("no such index and [index.mapper.dynamic] is [false]", e.getMessage());
        }
    });
    assertTrue(onFailureCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 37 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project camel by apache.

the class BulkRequestAggregationStrategy method aggregate.

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    // Don't use getBody(Class<T>) here as we don't want to coerce the body type using a type converter.
    Object objBody = newExchange.getIn().getBody();
    if (!(objBody instanceof ActionRequest)) {
        throw new InvalidPayloadRuntimeException(newExchange, ActionRequest.class);
    }
    ActionRequest newBody = (ActionRequest) objBody;
    BulkRequest request;
    if (oldExchange == null) {
        request = new BulkRequest();
        request.add(newBody);
        newExchange.getIn().setBody(request);
        return newExchange;
    } else {
        request = oldExchange.getIn().getBody(BulkRequest.class);
        request.add(newBody);
        return oldExchange;
    }
}
Also used : ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Example 38 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project camel by apache.

the class ElasticsearchBulkTest method bulkRequestBody.

@Test
public void bulkRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    BulkRequest request = new BulkRequest();
    request.add(new IndexRequest(prefix + "foo", prefix + "bar", prefix + "baz").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"));
    // when
    BulkResponse response = template.requestBody("direct:bulk", request, BulkResponse.class);
    // then
    assertThat(response, notNullValue());
    assertEquals(prefix + "baz", response.getItems()[0].getId());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) Test(org.junit.Test)

Example 39 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest in project camel by apache.

the class ElasticsearchBulkTest method bulkIndexRequestBody.

@Test
public void bulkIndexRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    BulkRequest request = new BulkRequest();
    request.add(new IndexRequest(prefix + "foo", prefix + "bar", prefix + "baz").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"));
    // when
    @SuppressWarnings("unchecked") List<String> indexedDocumentIds = template.requestBody("direct:bulk_index", request, List.class);
    // then
    assertThat(indexedDocumentIds, notNullValue());
    assertThat(indexedDocumentIds.size(), equalTo(1));
    assertThat(indexedDocumentIds, hasItem(prefix + "baz"));
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Test(org.junit.Test)

Example 40 with BulkRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest 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)

Aggregations

BulkRequest (org.elasticsearch.action.bulk.BulkRequest)50 IndexRequest (org.elasticsearch.action.index.IndexRequest)33 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)22 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)14 IOException (java.io.IOException)13 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)13 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 List (java.util.List)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)8 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)7 Pipeline (com.hazelcast.jet.pipeline.Pipeline)6 ActionListener (org.elasticsearch.action.ActionListener)6 ActionRequest (org.elasticsearch.action.ActionRequest)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 GetRequest (org.elasticsearch.action.get.GetRequest)5 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)5 Map (java.util.Map)4 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)4