Refine search
try (Writer writer = new FileWriter(filepath)) { final GetResponse response = tc.get(new GetRequest(index).type(type).id(id).refresh(true).realtime(false)).actionGet(); if (response.isExists()) { if(response.isSourceEmpty()) { System.out.println(" FAIL: Configuration for '"+_id+"' failed because of empty source"); return false; String yaml = convertToYaml(_id, response.getSourceAsBytesRef(), true); writer.write(yaml); System.out.println(" SUCC: Configuration for '"+_id+"' stored in "+filepath);
private static Request getStyleRequest(String method, GetRequest getRequest) { Request request = new Request(method, endpoint(getRequest.index(), getRequest.type(), getRequest.id())); Params parameters = new Params(request); parameters.withPreference(getRequest.preference()); parameters.withRouting(getRequest.routing()); parameters.withRefresh(getRequest.refresh()); parameters.withRealtime(getRequest.realtime()); parameters.withStoredFields(getRequest.storedFields()); parameters.withVersion(getRequest.version()); parameters.withVersionType(getRequest.versionType()); parameters.withFetchSourceContext(getRequest.fetchSourceContext()); return request; }
@Override public String toString() { StringBuilder message = new StringBuilder().append("ES get request"); message.append(String.format(" for key '%s'", request.id())); message.append(String.format(" on index '%s'", request.index())); message.append(String.format(" on type '%s'", request.type())); return message.toString(); }
public static boolean isNeeded(GetRequest request) { return (request.fetchSourceContext() != null && (request.fetchSourceContext().includes() != null || request.fetchSourceContext() .excludes() != null)) || (request.storedFields() != null && request.storedFields().length > 0); }
private static void checkExistanceOfDocument(GetRequestBuilder getRequestBuilder, GetResponse dataOfDocumentResponse) throws Error { if (!dataOfDocumentResponse.isExists()) { throw FailureHandler.createFailure( "Document with index: %s - type: %s - id: %s has not returned any document.", getRequestBuilder.request().index(), getRequestBuilder.request().type(), getRequestBuilder.request().id()); } }
@Override protected GetResponse shardOperation(GetRequest request, ShardId shardId) { IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex()); IndexShard indexShard = indexService.getShard(shardId.id()); if (request.refresh() && !request.realtime()) { indexShard.refresh("refresh_flag_get"); } GetResult result = indexShard.getService().get(request.type(), request.id(), request.storedFields(), request.realtime(), request.version(), request.versionType(), request.fetchSourceContext()); return new GetResponse(result); }
@Override public ESSearchHit get(String index, String id) throws IOException { GetRequest request = new GetRequest(index, id); GetResponse response = client.get(request, RequestOptions.DEFAULT); ESSearchHit hit = new ESSearchHit(); hit.setIndex(response.getIndex()); hit.setId(response.getId()); hit.setVersion(response.getVersion()); hit.setSourceAsMap(response.getSourceAsMap()); return hit; }
@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; }
@Override public ListenableFuture<ResponseType> act(GetRequest request) { UrlBuilder url = UrlBuilder.create() .protocol(protocol).host(host).port(port) .path(urlEncode(notNull(request.index()))) .seg(urlEncode(notNull(request.type()))) .seg(urlEncode(notNull(request.id()))) .paramIfPresent("refresh", fromNullable(request.refresh()).transform(booleanToString)) .paramIfPresent("routing", fromNullable(request.routing())) // note parent(string) seems just to set the routing, so we don't need to provide it here .paramIfPresent("preference", fromNullable(request.preference())) .paramIfPresent("realtime", fromNullable(request.realtime()).transform(booleanToString)) .paramIfPresent("ignore_errors_on_generated_fields", fromNullable(request.ignoreErrorsOnGeneratedFields()).transform(booleanToString)) .paramIfPresent("fields", fromNullable(request.fields()).transform(commaDelimitedToString)); return Futures.transform(executor.get(url.url()), responseTransform); } }
private static void checkDocumentEquality(Map<String, Object> expectedDataOfDocument, GetRequestBuilder getRequestBuilder, GetResponse dataOfDocumentResponse) { final Map<String, Object> dataOfDocument = new LinkedHashMap<>(dataOfDocumentResponse.getSource()); if (!DeepEquals.deepEquals(dataOfDocument, expectedDataOfDocument)) { try { throw FailureHandler.createFailure("Expected document for index: %s - type: %s - id: %s is %s, but %s was found.", getRequestBuilder.request().index(), getRequestBuilder.request().type(), getRequestBuilder.request().id(), OBJECT_MAPPER.writeValueAsString(expectedDataOfDocument), OBJECT_MAPPER.writeValueAsString(dataOfDocument)); } catch (IOException e) { throw new IllegalArgumentException(e); } } }
@Override public String get(String workflowInstanceId, String fieldToGet) { GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); GetResponse response; try { response = elasticSearchClient.get(request); } catch (IOException e) { logger.error("Unable to get Workflow: {} from ElasticSearch index: {}", workflowInstanceId, indexName, e); return null; } 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; }
public static Request sourceExists(GetRequest getRequest) { Request request = new Request(HttpMethod.HEAD.name(), endpoint(getRequest.index(), getRequest.type(), getRequest.id(), "_source")); Params parameters = new Params(request); parameters.withPreference(getRequest.preference()); parameters.withRouting(getRequest.routing()); parameters.withRefresh(getRequest.refresh()); parameters.withRealtime(getRequest.realtime()); // Version params are not currently supported by the source exists API so are not passed return request; }
try { GetResponse response = testClient.get(new GetRequest(indexName, indexType, id1)); Assert.assertEquals(response.getId(), id1, "Response id matches request"); System.out.println(malformedDocPolicy + ":" + response.toString()); if (response.isExists()) { docsIndexed++; GetResponse response = testClient.get(new GetRequest(indexName, indexType, id2)); Assert.assertEquals(response.getId(), id2, "Response id matches request"); System.out.println(malformedDocPolicy + ":" + response.toString());
@Override public void onResponse(GetResponse response) { try { if (!response.isExists()) { throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] not found"); if (response.isSourceEmpty()) { throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] source disabled"); LoggingDeprecationHandler.INSTANCE, response.getSourceAsBytesRef())) { XContentParser.Token currentToken; while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { throw new IllegalStateException("Shape with name [" + getRequest.id() + "] found but missing " + path + " field");
GetResponse response = testClient.get(new GetRequest(indexName, indexType, id)); Assert.assertEquals(response.getId(), id, "Response id matches request"); Assert.assertEquals(response.isExists(), true, "Document not found"); } catch (Exception e) { Assert.fail("Failed to get a response", e);
@Override protected void resolveRequest(ClusterState state, InternalRequest request) { IndexMetaData indexMeta = state.getMetaData().index(request.concreteIndex()); // update the routing (request#index here is possibly an alias) request.request().routing(state.metaData().resolveIndexRouting(request.request().parent(), request.request().routing(), request.request().index())); // Fail fast on the node that received the request. if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) { throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id()); } }
@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.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())); if (request.param("fields") != null) { throw new IllegalArgumentException("the parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or [_source] to load the field from _source"); } final String fieldsParam = request.param("stored_fields"); if (fieldsParam != null) { final String[] fields = Strings.splitStringByCommaToArray(fieldsParam); if (fields != null) { getRequest.storedFields(fields); } } getRequest.version(RestActions.parseVersion(request)); getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType())); getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request)); return channel -> client.get(getRequest, new RestToXContentListener<GetResponse>(channel) { @Override protected RestStatus getStatus(final GetResponse response) { return response.isExists() ? OK : NOT_FOUND; } }); }
Assert.assertTrue(tc.get(new GetRequest("searchguard", "sg", "config")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","internalusers")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","roles")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","rolesmapping")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","actiongroups")).actionGet().isExists()); Assert.assertFalse(tc.get(new GetRequest("searchguard","sg","rolesmapping_xcvdnghtu165759i99465")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","config")).actionGet().isExists());
@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.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 RestGetSourceResponseListener(channel, request)); } }; }
void parseExistingDocPercolate(PercolateRequest percolateRequest, RestRequest restRequest, RestChannel restChannel, final Client client) { String index = restRequest.param("index"); String type = restRequest.param("type"); percolateRequest.indices(Strings.splitStringByCommaToArray(restRequest.param("percolate_index", index))); percolateRequest.documentType(restRequest.param("percolate_type", type)); GetRequest getRequest = new GetRequest(index, type, restRequest.param("id")); getRequest.routing(restRequest.param("routing")); getRequest.preference(restRequest.param("preference")); getRequest.refresh(restRequest.paramAsBoolean("refresh", getRequest.refresh())); getRequest.realtime(restRequest.paramAsBoolean("realtime", null)); getRequest.version(RestActions.parseVersion(restRequest)); getRequest.versionType(VersionType.fromString(restRequest.param("version_type"), getRequest.versionType())); percolateRequest.getRequest(getRequest); percolateRequest.routing(restRequest.param("percolate_routing")); percolateRequest.preference(restRequest.param("percolate_preference")); percolateRequest.source(RestActions.getRestContent(restRequest)); percolateRequest.indicesOptions(IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions())); executePercolate(percolateRequest, restChannel, client); }