Java Code Examples for org.elasticsearch.action.get.GetRequest#preference()
The following examples show how to use
org.elasticsearch.action.get.GetRequest#preference() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestHeadAction.java From Elasticsearch with Apache License 2.0 | 6 votes |
@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); } } }); }
Example 2
Source File: RestPercolateAction.java From Elasticsearch with Apache License 2.0 | 6 votes |
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); }
Example 3
Source File: ShapeFetchService.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * Fetches the Shape with the given ID in the given type and index. * * @param getRequest GetRequest containing index, type and id * @param path Name or path of the field in the Shape Document where the Shape itself is located * @return Shape with the given ID * @throws IOException Can be thrown while parsing the Shape Document and extracting the Shape */ public ShapeBuilder fetch(GetRequest getRequest,String path) throws IOException { getRequest.preference("_local"); getRequest.operationThreaded(false); GetResponse response = client.get(getRequest).actionGet(); if (!response.isExists()) { throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] not found"); } String[] pathElements = Strings.splitStringToArray(path, '.'); int currentPathSlot = 0; XContentParser parser = null; try { parser = XContentHelper.createParser(response.getSourceAsBytesRef()); XContentParser.Token currentToken; while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (currentToken == XContentParser.Token.FIELD_NAME) { if (pathElements[currentPathSlot].equals(parser.currentName())) { parser.nextToken(); if (++currentPathSlot == pathElements.length) { return ShapeBuilder.parse(parser); } } else { parser.nextToken(); parser.skipChildren(); } } } throw new IllegalStateException("Shape with name [" + getRequest.id() + "] found but missing " + path + " field"); } finally { if (parser != null) { parser.close(); } } }
Example 4
Source File: RestGetAction.java From Elasticsearch with Apache License 2.0 | 5 votes |
@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)); getRequest.ignoreErrorsOnGeneratedFields(request.paramAsBoolean("ignore_errors_on_generated_fields", false)); String sField = request.param("fields"); if (sField != null) { String[] sFields = Strings.splitStringByCommaToArray(sField); if (sFields != null) { getRequest.fields(sFields); } } getRequest.version(RestActions.parseVersion(request)); getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType())); getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request)); client.get(getRequest, new RestBuilderListener<GetResponse>(channel) { @Override public RestResponse buildResponse(GetResponse response, XContentBuilder builder) throws Exception { builder.startObject(); response.toXContent(builder, request); builder.endObject(); if (!response.isExists()) { return new BytesRestResponse(NOT_FOUND, builder); } else { return new BytesRestResponse(OK, builder); } } }); }
Example 5
Source File: RestGetSourceAction.java From Elasticsearch with Apache License 2.0 | 5 votes |
@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)); getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request)); if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) { try { ActionRequestValidationException validationError = new ActionRequestValidationException(); validationError.addValidationError("fetching source can not be disabled"); channel.sendResponse(new BytesRestResponse(channel, validationError)); } catch (IOException e) { logger.error("Failed to send failure response", e); } } client.get(getRequest, new RestResponseListener<GetResponse>(channel) { @Override public RestResponse buildResponse(GetResponse response) throws Exception { XContentBuilder builder = channel.newBuilder(response.getSourceInternal(), false); if (!response.isExists()) { return new BytesRestResponse(NOT_FOUND, builder); } else { builder.rawValue(response.getSourceInternal()); return new BytesRestResponse(OK, builder); } } }); }