Search in sources :

Example 11 with GetRequest

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

the class RestGetSourceAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    getRequest.operationThreaded(true);
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.parent(request.param("parent"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> {
        if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) {
            final ActionRequestValidationException validationError = new ActionRequestValidationException();
            validationError.addValidationError("fetching source can not be disabled");
            channel.sendResponse(new BytesRestResponse(channel, validationError));
        } else {
            client.get(getRequest, new RestResponseListener<GetResponse>(channel) {

                @Override
                public RestResponse buildResponse(final GetResponse response) throws Exception {
                    final XContentBuilder builder = channel.newBuilder(request.getXContentType(), false);
                    if (response.isSourceEmpty()) {
                        return new BytesRestResponse(NOT_FOUND, builder);
                    } else {
                        builder.rawValue(response.getSourceInternal());
                        return new BytesRestResponse(OK, builder);
                    }
                }
            });
        }
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) GetRequest(org.elasticsearch.action.get.GetRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with GetRequest

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

the class CrudIT method testExists.

public void testExists() throws IOException {
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
    StringEntity stringEntity = new StringEntity(document, ContentType.APPLICATION_JSON);
    Response response = client().performRequest("PUT", "/index/type/id", Collections.singletonMap("refresh", "wait_for"), stringEntity);
    assertEquals(201, response.getStatusLine().getStatusCode());
    {
        GetRequest getRequest = new GetRequest("index", "type", "id");
        assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "does_not_exist");
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
    {
        GetRequest getRequest = new GetRequest("index", "type", "does_not_exist").version(1);
        assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
    }
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) StringEntity(org.apache.http.entity.StringEntity) GetRequest(org.elasticsearch.action.get.GetRequest)

Example 13 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 14 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 15 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)

Aggregations

GetRequest (org.elasticsearch.action.get.GetRequest)17 GetResponse (org.elasticsearch.action.get.GetResponse)12 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)3 List (java.util.List)2 Message (org.apache.camel.Message)2 StringEntity (org.apache.http.entity.StringEntity)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)2 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)2 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)2