Search in sources :

Example 21 with GetRequest

use of org.elasticsearch.action.get.GetRequest in project incubator-gobblin by apache.

the class RestWriterVariant method getTestClient.

@Override
public TestClient getTestClient(Config config) throws IOException {
    final ElasticsearchRestWriter restWriter = new ElasticsearchRestWriter(config);
    final RestHighLevelClient highLevelClient = restWriter.getRestHighLevelClient();
    return new TestClient() {

        @Override
        public GetResponse get(GetRequest getRequest) throws IOException {
            return highLevelClient.get(getRequest);
        }

        @Override
        public void recreateIndex(String indexName) throws IOException {
            RestClient restClient = restWriter.getRestLowLevelClient();
            try {
                restClient.performRequest("DELETE", "/" + indexName);
            } catch (Exception e) {
            // ok since index may not exist
            }
            String indexSettings = "{\"settings\" : {\"index\":{\"number_of_shards\":1,\"number_of_replicas\":1}}}";
            HttpEntity entity = new StringEntity(indexSettings, ContentType.APPLICATION_JSON);
            Response putResponse = restClient.performRequest("PUT", "/" + indexName, Collections.emptyMap(), entity);
            Assert.assertEquals(putResponse.getStatusLine().getStatusCode(), 200, "Recreate index succeeded");
        }

        @Override
        public void close() throws IOException {
            restWriter.close();
        }
    };
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) Response(org.elasticsearch.client.Response) StringEntity(org.apache.http.entity.StringEntity) HttpEntity(org.apache.http.HttpEntity) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException)

Example 22 with GetRequest

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

the class TermsQueryBuilder method fetch.

private List<Object> fetch(TermsLookup termsLookup, Client client) {
    List<Object> terms = new ArrayList<>();
    GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.type(), termsLookup.id()).preference("_local").routing(termsLookup.routing());
    final GetResponse getResponse = client.get(getRequest).actionGet();
    if (getResponse.isSourceEmpty() == false) {
        // extract terms only if the doc source exists
        List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap());
        terms.addAll(extractedValues);
    }
    return terms;
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 23 with GetRequest

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

the class TransportGetTaskAction method getFinishedTaskFromIndex.

/**
     * Send a {@link GetRequest} to the tasks index looking for a persisted copy of the task completed task. It'll only be found only if the
     * task's result was stored. Called on the node that once had the task if that node is still part of the cluster or on the
     * coordinating node if the node is no longer part of the cluster.
     */
void getFinishedTaskFromIndex(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) {
    GetRequest get = new GetRequest(TaskResultsService.TASK_INDEX, TaskResultsService.TASK_TYPE, request.getTaskId().toString());
    get.setParentTask(clusterService.localNode().getId(), thisTask.getId());
    client.get(get, new ActionListener<GetResponse>() {

        @Override
        public void onResponse(GetResponse getResponse) {
            try {
                onGetFinishedTaskFromIndex(getResponse, listener);
            } catch (Exception e) {
                listener.onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            if (ExceptionsHelper.unwrap(e, IndexNotFoundException.class) != null) {
                // We haven't yet created the index for the task results so it can't be found.
                listener.onFailure(new ResourceNotFoundException("task [{}] isn't running and hasn't stored its results", e, request.getTaskId()));
            } else {
                listener.onFailure(e);
            }
        }
    });
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) GetResponse(org.elasticsearch.action.get.GetResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Example 24 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 25 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)

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