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));
}
}
use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.
the class RequestTests method getAndExistsTest.
private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
String index = randomAsciiOfLengthBetween(3, 10);
String type = randomAsciiOfLengthBetween(3, 10);
String id = randomAsciiOfLengthBetween(3, 10);
GetRequest getRequest = new GetRequest(index, type, id);
Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) {
if (randomBoolean()) {
String preference = randomAsciiOfLengthBetween(3, 10);
getRequest.preference(preference);
expectedParams.put("preference", preference);
}
if (randomBoolean()) {
String routing = randomAsciiOfLengthBetween(3, 10);
getRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
boolean realtime = randomBoolean();
getRequest.realtime(realtime);
if (realtime == false) {
expectedParams.put("realtime", "false");
}
}
if (randomBoolean()) {
boolean refresh = randomBoolean();
getRequest.refresh(refresh);
if (refresh) {
expectedParams.put("refresh", "true");
}
}
if (randomBoolean()) {
long version = randomLong();
getRequest.version(version);
if (version != Versions.MATCH_ANY) {
expectedParams.put("version", Long.toString(version));
}
}
if (randomBoolean()) {
VersionType versionType = randomFrom(VersionType.values());
getRequest.versionType(versionType);
if (versionType != VersionType.INTERNAL) {
expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
}
}
if (randomBoolean()) {
int numStoredFields = randomIntBetween(1, 10);
String[] storedFields = new String[numStoredFields];
StringBuilder storedFieldsParam = new StringBuilder();
for (int i = 0; i < numStoredFields; i++) {
String storedField = randomAsciiOfLengthBetween(3, 10);
storedFields[i] = storedField;
storedFieldsParam.append(storedField);
if (i < numStoredFields - 1) {
storedFieldsParam.append(",");
}
}
getRequest.storedFields(storedFields);
expectedParams.put("stored_fields", storedFieldsParam.toString());
}
if (randomBoolean()) {
randomizeFetchSourceContextParams(getRequest::fetchSourceContext, expectedParams);
}
}
Request request = requestConverter.apply(getRequest);
assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
assertEquals(expectedParams, request.params);
assertNull(request.entity);
assertEquals(method, request.method);
}
use of org.elasticsearch.action.get.GetRequest in project elasticsearch by elastic.
the class IndicesRequestIT method testGet.
public void testGet() {
String getShardAction = GetAction.NAME + "[s]";
interceptTransportActions(getShardAction);
GetRequest getRequest = new GetRequest(randomIndexOrAlias(), "type", "id");
internalCluster().coordOnlyNodeClient().get(getRequest).actionGet();
clearInterceptedActions();
assertSameIndices(getRequest, getShardAction);
}
use of org.elasticsearch.action.get.GetRequest in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method getRequestBody.
@Test
public void getRequestBody() throws Exception {
String prefix = createPrefix();
// given
GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar");
// when
String documentId = template.requestBody("direct:index", new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
GetResponse response = template.requestBody("direct:get", request.id(documentId), GetResponse.class);
// then
assertThat(response, notNullValue());
assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content")));
}
Aggregations