Change HLRC SourceExists to use GetSourceRequest instead of GetRequest (#51789) (#51913)

Originates from #50885

Co-authored-by: Maxim <timonin.maksim@mail.ru>
This commit is contained in:
Martijn van Groningen 2020-02-05 13:27:31 +01:00 committed by GitHub
parent 64f9a2089b
commit 0610eb51ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 35 deletions

View File

@ -281,28 +281,15 @@ final class RequestConverters {
return request; return request;
} }
static Request sourceExists(GetRequest getRequest) { static Request sourceExists(GetSourceRequest getSourceRequest) {
Params parameters = new Params(); return sourceRequest(getSourceRequest, HttpHead.METHOD_NAME);
parameters.withPreference(getRequest.preference());
parameters.withRouting(getRequest.routing());
parameters.withRefresh(getRequest.refresh());
parameters.withRealtime(getRequest.realtime());
parameters.withFetchSourceContext(getRequest.fetchSourceContext());
// Version params are not currently supported by the _source API so are not passed
String optionalType = getRequest.type();
String endpoint;
if (optionalType.equals(MapperService.SINGLE_MAPPING_NAME)) {
endpoint = endpoint(getRequest.index(), "_source", getRequest.id());
} else {
endpoint = endpoint(getRequest.index(), optionalType, getRequest.id(), "_source");
}
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
request.addParameters(parameters.asMap());
return request;
} }
static Request getSource(GetSourceRequest getSourceRequest) { static Request getSource(GetSourceRequest getSourceRequest) {
return sourceRequest(getSourceRequest, HttpGet.METHOD_NAME);
}
private static Request sourceRequest(GetSourceRequest getSourceRequest, String httpMethodName) {
Params parameters = new Params(); Params parameters = new Params();
parameters.withPreference(getSourceRequest.preference()); parameters.withPreference(getSourceRequest.preference());
parameters.withRouting(getSourceRequest.routing()); parameters.withRouting(getSourceRequest.routing());
@ -310,8 +297,14 @@ final class RequestConverters {
parameters.withRealtime(getSourceRequest.realtime()); parameters.withRealtime(getSourceRequest.realtime());
parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext()); parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext());
String endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id()); String optionalType = getSourceRequest.type();
Request request = new Request(HttpGet.METHOD_NAME, endpoint); String endpoint;
if (optionalType == null) {
endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
} else {
endpoint = endpoint(getSourceRequest.index(), optionalType, getSourceRequest.id(), "_source");
}
Request request = new Request(httpMethodName, endpoint);
request.addParameters(parameters.asMap()); request.addParameters(parameters.asMap());
return request; return request;
} }

View File

@ -843,9 +843,13 @@ public class RestHighLevelClient implements Closeable {
* @param getRequest the request * @param getRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise * @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise
* @deprecated use {@link #existsSource(GetSourceRequest, RequestOptions)} instead
*/ */
@Deprecated
public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException { public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException {
return performRequest(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, emptySet()); GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest);
return performRequest(getSourceRequest, RequestConverters::sourceExists, options,
RestHighLevelClient::convertExistsResponse, emptySet());
} }
/** /**
@ -856,9 +860,40 @@ public class RestHighLevelClient implements Closeable {
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion * @param listener the listener to be notified upon request completion
* @return cancellable that may be used to cancel the request * @return cancellable that may be used to cancel the request
* @deprecated use {@link #existsSourceAsync(GetSourceRequest, RequestOptions, ActionListener)} instead
*/ */
@Deprecated
public final Cancellable existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener<Boolean> listener) { public final Cancellable existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener<Boolean> listener) {
return performRequestAsync(getRequest, RequestConverters::sourceExists, options, GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest);
return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options,
RestHighLevelClient::convertExistsResponse, listener, emptySet());
}
/**
* Checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
* on elastic.co</a>
* @param getSourceRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise
*/
public boolean existsSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException {
return performRequest(getSourceRequest, RequestConverters::sourceExists, options,
RestHighLevelClient::convertExistsResponse, emptySet());
}
/**
* Asynchronously checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
* on elastic.co</a>
* @param getSourceRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
* @return cancellable that may be used to cancel the request
*/
public final Cancellable existsSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options,
ActionListener<Boolean> listener) {
return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options,
RestHighLevelClient::convertExistsResponse, listener, emptySet()); RestHighLevelClient::convertExistsResponse, listener, emptySet());
} }
@ -866,12 +901,12 @@ public class RestHighLevelClient implements Closeable {
* Retrieves the source field only of a document using GetSource API. * Retrieves the source field only of a document using GetSource API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API
* on elastic.co</a> * on elastic.co</a>
* @param getRequest the request * @param getSourceRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response * @return the response
*/ */
public GetSourceResponse getSource(GetSourceRequest getRequest, RequestOptions options) throws IOException { public GetSourceResponse getSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException {
return performRequestAndParseEntity(getRequest, RequestConverters::getSource, options, return performRequestAndParseEntity(getSourceRequest, RequestConverters::getSource, options,
GetSourceResponse::fromXContent, emptySet()); GetSourceResponse::fromXContent, emptySet());
} }
@ -879,14 +914,14 @@ public class RestHighLevelClient implements Closeable {
* Asynchronously retrieves the source field only of a document using GetSource API. * Asynchronously retrieves the source field only of a document using GetSource API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API
* on elastic.co</a> * on elastic.co</a>
* @param getRequest the request * @param getSourceRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion * @param listener the listener to be notified upon request completion
* @return cancellable that may be used to cancel the request * @return cancellable that may be used to cancel the request
*/ */
public final Cancellable getSourceAsync(GetSourceRequest getRequest, RequestOptions options, public final Cancellable getSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options,
ActionListener<GetSourceResponse> listener) { ActionListener<GetSourceResponse> listener) {
return performRequestAsyncAndParseEntity(getRequest, RequestConverters::getSource, options, return performRequestAsyncAndParseEntity(getSourceRequest, RequestConverters::getSource, options,
GetSourceResponse::fromXContent, listener, emptySet()); GetSourceResponse::fromXContent, listener, emptySet());
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.client.core; package org.elasticsearch.client.core;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.client.Validatable; import org.elasticsearch.client.Validatable;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -36,6 +37,7 @@ public final class GetSourceRequest implements Validatable, ToXContentObject {
private FetchSourceContext fetchSourceContext; private FetchSourceContext fetchSourceContext;
private String index; private String index;
private String type;
private String id; private String id;
public GetSourceRequest(String index, String id) { public GetSourceRequest(String index, String id) {
@ -43,6 +45,15 @@ public final class GetSourceRequest implements Validatable, ToXContentObject {
this.id = id; this.id = id;
} }
public static GetSourceRequest from(GetRequest getRequest) {
return new GetSourceRequest(getRequest.index(), getRequest.id())
.routing(getRequest.routing())
.preference(getRequest.preference())
.refresh(getRequest.refresh())
.realtime(getRequest.realtime())
.fetchSourceContext(getRequest.fetchSourceContext());
}
/** /**
* Controls the shard routing of the request. Using this value to hash the shard * Controls the shard routing of the request. Using this value to hash the shard
* and not the id. * and not the id.
@ -100,6 +111,15 @@ public final class GetSourceRequest implements Validatable, ToXContentObject {
return index; return index;
} }
public String type() {
return type;
}
public GetSourceRequest type(String type) {
this.type = type;
return this;
}
public String id() { public String id() {
return id; return id;
} }

View File

@ -213,7 +213,9 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
} }
} }
public void testSourceExists() throws IOException { // used deprecated API existsSource(GetRequest, RequestOptions)
// see test `testSourceExists` with new API tests
public void testDeprecatedSourceExists() throws IOException {
{ {
GetRequest getRequest = new GetRequest("index", "id"); GetRequest getRequest = new GetRequest("index", "id");
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
@ -236,6 +238,25 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
} }
} }
public void testSourceExists() throws IOException {
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
IndexRequest index = new IndexRequest("index").id("id");
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
highLevelClient().index(index, RequestOptions.DEFAULT);
{
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
{
GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
}
}
public void testSourceDoesNotExist() throws IOException { public void testSourceDoesNotExist() throws IOException {
final String noSourceIndex = "no_source"; final String noSourceIndex = "no_source";
{ {
@ -262,7 +283,11 @@ public class CrudIT extends ESRestHighLevelClientTestCase {
{ {
GetRequest getRequest = new GetRequest(noSourceIndex, "1"); GetRequest getRequest = new GetRequest(noSourceIndex, "1");
assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync)); assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
// used deprecated API existsSource(GetRequest, RequestOptions)
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
// used new API existsSource(GetSourceRequest, RequestOptions)
GetSourceRequest getSourceRequest = new GetSourceRequest(noSourceIndex, "1");
assertFalse(execute(getSourceRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
} }
} }

View File

@ -165,22 +165,22 @@ public class RequestConvertersTests extends ESTestCase {
} }
public void testSourceExists() throws IOException { public void testSourceExists() throws IOException {
doTestSourceExists((index, id) -> new GetRequest(index, id)); doTestSourceExists((index, id) -> new GetSourceRequest(index, id));
} }
public void testSourceExistsWithType() throws IOException { public void testSourceExistsWithType() throws IOException {
String type = frequently() ? randomAlphaOfLengthBetween(3, 10) : MapperService.SINGLE_MAPPING_NAME; String type = frequently() ? randomAlphaOfLengthBetween(3, 10) : MapperService.SINGLE_MAPPING_NAME;
doTestSourceExists((index, id) -> new GetRequest(index, type, id)); doTestSourceExists((index, id) -> new GetSourceRequest(index, id).type(type));
} }
public void testGetSource() throws IOException { public void testGetSource() throws IOException {
doTestGetSource((index, id) -> new GetSourceRequest(index, id)); doTestGetSource((index, id) -> new GetSourceRequest(index, id));
} }
private static void doTestSourceExists(BiFunction<String, String, GetRequest> requestFunction) throws IOException { private static void doTestSourceExists(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
String index = randomAlphaOfLengthBetween(3, 10); String index = randomAlphaOfLengthBetween(3, 10);
String id = randomAlphaOfLengthBetween(3, 10); String id = randomAlphaOfLengthBetween(3, 10);
final GetRequest getRequest = requestFunction.apply(index, id); final GetSourceRequest getRequest = requestFunction.apply(index, id);
Map<String, String> expectedParams = new HashMap<>(); Map<String, String> expectedParams = new HashMap<>();
if (randomBoolean()) { if (randomBoolean()) {
@ -210,7 +210,7 @@ public class RequestConvertersTests extends ESTestCase {
Request request = RequestConverters.sourceExists(getRequest); Request request = RequestConverters.sourceExists(getRequest);
assertEquals(HttpHead.METHOD_NAME, request.getMethod()); assertEquals(HttpHead.METHOD_NAME, request.getMethod());
String type = getRequest.type(); String type = getRequest.type();
if (type.equals(MapperService.SINGLE_MAPPING_NAME)) { if (type == null) {
assertEquals("/" + index + "/_source/" + id, request.getEndpoint()); assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
} else { } else {
assertEquals("/" + index + "/" + type + "/" + id + "/_source", request.getEndpoint()); assertEquals("/" + index + "/" + type + "/" + id + "/_source", request.getEndpoint());