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();
}
};
}
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;
}
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);
}
}
});
}
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);
}
}
});
}
};
}
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));
}
}
Aggregations