Search in sources :

Example 26 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class RequestTests method getAndExistsTest.

private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    GetRequest getRequest = new GetRequest(index, type, id);
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        if (randomBoolean()) {
            String preference = randomAsciiOfLengthBetween(3, 10);
            getRequest.preference(preference);
            expectedParams.put("preference", preference);
        }
        if (randomBoolean()) {
            String routing = randomAsciiOfLengthBetween(3, 10);
            getRequest.routing(routing);
            expectedParams.put("routing", routing);
        }
        if (randomBoolean()) {
            boolean realtime = randomBoolean();
            getRequest.realtime(realtime);
            if (realtime == false) {
                expectedParams.put("realtime", "false");
            }
        }
        if (randomBoolean()) {
            boolean refresh = randomBoolean();
            getRequest.refresh(refresh);
            if (refresh) {
                expectedParams.put("refresh", "true");
            }
        }
        if (randomBoolean()) {
            long version = randomLong();
            getRequest.version(version);
            if (version != Versions.MATCH_ANY) {
                expectedParams.put("version", Long.toString(version));
            }
        }
        if (randomBoolean()) {
            VersionType versionType = randomFrom(VersionType.values());
            getRequest.versionType(versionType);
            if (versionType != VersionType.INTERNAL) {
                expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
            }
        }
        if (randomBoolean()) {
            int numStoredFields = randomIntBetween(1, 10);
            String[] storedFields = new String[numStoredFields];
            StringBuilder storedFieldsParam = new StringBuilder();
            for (int i = 0; i < numStoredFields; i++) {
                String storedField = randomAsciiOfLengthBetween(3, 10);
                storedFields[i] = storedField;
                storedFieldsParam.append(storedField);
                if (i < numStoredFields - 1) {
                    storedFieldsParam.append(",");
                }
            }
            getRequest.storedFields(storedFields);
            expectedParams.put("stored_fields", storedFieldsParam.toString());
        }
        if (randomBoolean()) {
            randomizeFetchSourceContextParams(getRequest::fetchSourceContext, expectedParams);
        }
    }
    Request request = requestConverter.apply(getRequest);
    assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
    assertEquals(expectedParams, request.params);
    assertNull(request.entity);
    assertEquals(method, request.method);
}
Also used : HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) VersionType(org.elasticsearch.index.VersionType)

Example 27 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.

the class IndicesRequestIT method testGet.

public void testGet() {
    String getShardAction = GetAction.NAME + "[s]";
    interceptTransportActions(getShardAction);
    GetRequest getRequest = new GetRequest(randomIndexOrAlias(), "type", "id");
    internalCluster().coordOnlyNodeClient().get(getRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(getRequest, getShardAction);
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 28 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method getRequestBody.

@Test
public void getRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar");
    // when
    String documentId = template.requestBody("direct:index", new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
    GetResponse response = template.requestBody("direct:get", request.id(documentId), GetResponse.class);
    // then
    assertThat(response, notNullValue());
    assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content")));
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Test(org.junit.Test)

Example 29 with GetRequest

use of org.elasticsearch.action.get.GetRequest 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 30 with GetRequest

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

GetRequest (org.elasticsearch.action.get.GetRequest)45 GetResponse (org.elasticsearch.action.get.GetResponse)29 Test (org.junit.Test)14 IOException (java.io.IOException)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)9 HashMap (java.util.HashMap)7 TikaDocument (org.icij.extract.document.TikaDocument)7 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)6 ArrayList (java.util.ArrayList)5 DocumentFactory (org.icij.extract.document.DocumentFactory)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ParsingReader (org.apache.tika.parser.ParsingReader)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 PathIdentifier (org.icij.extract.document.PathIdentifier)4 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3