Search in sources :

Example 6 with MultiGetRequest

use of org.elasticsearch.action.get.MultiGetRequest 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)

Aggregations

MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)6 List (java.util.List)4 ArrayList (java.util.ArrayList)3 Converter (org.apache.camel.Converter)2 Message (org.apache.camel.Message)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 Item (org.elasticsearch.action.get.MultiGetRequest.Item)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)2 IOException (java.io.IOException)1 ExistsRequest (org.elasticsearch.action.exists.ExistsRequest)1 Client (org.elasticsearch.client.Client)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 Strings (org.elasticsearch.common.Strings)1