@Override public String get(String workflowInstanceId, String fieldToGet) { GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId) .fetchSourceContext( new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY)); GetResponse response = elasticSearchClient.get(request).actionGet(); if (response.isExists()) { Map<String, Object> sourceAsMap = response.getSourceAsMap(); if (sourceAsMap.containsKey(fieldToGet)) { return sourceAsMap.get(fieldToGet).toString(); } } logger.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, indexName); return null; }
try (Writer writer = new FileWriter(filepath)) { final GetResponse response = tc.get(new GetRequest(index).type(type).id(id).refresh(true).realtime(false)).actionGet();
/** * Verify the results in an Elasticsearch index. The results must first be produced into the index * using a {@link TestElasticsearchSinkFunction}; * * @param client The client to use to connect to Elasticsearch * @param index The index to check */ public static void verifyProducedSinkData(Client client, String index) { for (int i = 0; i < NUM_ELEMENTS; i++) { GetResponse response = client.get(new GetRequest(index, TYPE_NAME, Integer.toString(i))).actionGet(); Assert.assertEquals(DATA_PREFIX + i, response.getSource().get(DATA_FIELD_NAME)); } }
client.get(getRequest, new ActionListener<GetResponse>(){
@Override public void updateOrCreate(PhotonDoc updatedDoc) { final boolean exists = this.esClient.get(this.esClient.prepareGet("photon", "place", String.valueOf(updatedDoc.getPlaceId())).request()).actionGet().isExists(); if (exists) { this.update(updatedDoc); } else { this.create(updatedDoc); } }
private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) { GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.type(), termsLookup.id()) .preference("_local").routing(termsLookup.routing()); client.get(getRequest, new ActionListener<GetResponse>() { @Override public void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); } @Override public void onFailure(Exception e) { actionListener.onFailure(e); } }); }
/** * 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); } } }); }
@Override protected ActionFuture<GetResponse> doExecute(GetRequest request) { return client.get(request); } }
Client client = new Client(Protocol.HTTP); client.get("http://127.0.0.1").getEntity().write(System.out);
Client client = new Client(Protocol.HTTP); Response response = client.get(url); if (response.getStatus().isError()) { // uh oh! }
@Override public GetResponse get(GetRequest request) { return client.get(request).actionGet(); }
String getScriptFromIndex(String scriptLang, String id, HasContextAndHeaders context) { if (client == null) { throw new IllegalArgumentException("Got an indexed script with no Client registered."); } scriptLang = validateScriptLanguage(scriptLang); GetRequest getRequest = new GetRequest(SCRIPT_INDEX, scriptLang, id); getRequest.copyContextAndHeadersFrom(context); GetResponse responseFields = client.get(getRequest).actionGet(); if (responseFields.isExists()) { return getScriptFromResponse(responseFields); } throw new IllegalArgumentException("Unable to find script [" + SCRIPT_INDEX + "/" + scriptLang + "/" + id + "]"); }
try (Writer writer = new FileWriter(filepath)) { final GetResponse response = tc.get(new GetRequest(index).type(type).id(id).refresh(true).realtime(false)).actionGet();
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; }
private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) { GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.type(), termsLookup.id()) .preference("_local").routing(termsLookup.routing()); client.get(getRequest, new ActionListener<GetResponse>() { @Override public void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); } @Override public void onFailure(Exception e) { actionListener.onFailure(e); } }); }
private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) { GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.type(), termsLookup.id()) .preference("_local").routing(termsLookup.routing()); client.get(getRequest, new ActionListener<GetResponse>() { @Override public void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); } @Override public void onFailure(Exception e) { actionListener.onFailure(e); } }); }
/** * 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); } } }); }
/** * 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); } } }); }
public void queryScriptIndex(GetIndexedScriptRequest request, final ActionListener<GetResponse> listener) { String scriptLang = validateScriptLanguage(request.scriptLang()); GetRequest getRequest = new GetRequest(request, SCRIPT_INDEX).type(scriptLang).id(request.id()) .version(request.version()).versionType(request.versionType()) .preference("_local"); //Set preference for no forking client.get(getRequest, listener); }
@Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { 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")); // order is important, set it after routing, so it will set the routing getRequest.parent(request.param("parent")); getRequest.preference(request.param("preference")); getRequest.realtime(request.paramAsBoolean("realtime", null)); // don't get any fields back... getRequest.fields(Strings.EMPTY_ARRAY); // TODO we can also just return the document size as Content-Length client.get(getRequest, new RestResponseListener<GetResponse>(channel) { @Override public RestResponse buildResponse(GetResponse response) { if (!response.isExists()) { return new BytesRestResponse(NOT_FOUND); } else { return new BytesRestResponse(OK); } } }); } }