Skip to content

Commit 0610eb5

Browse files
Change HLRC SourceExists to use GetSourceRequest instead of GetRequest (#51789) (#51913)
Originates from #50885 Co-authored-by: Maxim <[email protected]>
1 parent 64f9a20 commit 0610eb5

File tree

5 files changed

+108
-35
lines changed

5 files changed

+108
-35
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,37 +281,30 @@ private static Request getStyleRequest(String method, GetRequest getRequest) {
281281
return request;
282282
}
283283

284-
static Request sourceExists(GetRequest getRequest) {
285-
Params parameters = new Params();
286-
parameters.withPreference(getRequest.preference());
287-
parameters.withRouting(getRequest.routing());
288-
parameters.withRefresh(getRequest.refresh());
289-
parameters.withRealtime(getRequest.realtime());
290-
parameters.withFetchSourceContext(getRequest.fetchSourceContext());
291-
// Version params are not currently supported by the _source API so are not passed
292-
293-
String optionalType = getRequest.type();
294-
String endpoint;
295-
if (optionalType.equals(MapperService.SINGLE_MAPPING_NAME)) {
296-
endpoint = endpoint(getRequest.index(), "_source", getRequest.id());
297-
} else {
298-
endpoint = endpoint(getRequest.index(), optionalType, getRequest.id(), "_source");
299-
}
300-
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
301-
request.addParameters(parameters.asMap());
302-
return request;
284+
static Request sourceExists(GetSourceRequest getSourceRequest) {
285+
return sourceRequest(getSourceRequest, HttpHead.METHOD_NAME);
303286
}
304287

305288
static Request getSource(GetSourceRequest getSourceRequest) {
289+
return sourceRequest(getSourceRequest, HttpGet.METHOD_NAME);
290+
}
291+
292+
private static Request sourceRequest(GetSourceRequest getSourceRequest, String httpMethodName) {
306293
Params parameters = new Params();
307294
parameters.withPreference(getSourceRequest.preference());
308295
parameters.withRouting(getSourceRequest.routing());
309296
parameters.withRefresh(getSourceRequest.refresh());
310297
parameters.withRealtime(getSourceRequest.realtime());
311298
parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext());
312299

313-
String endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
314-
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
300+
String optionalType = getSourceRequest.type();
301+
String endpoint;
302+
if (optionalType == null) {
303+
endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
304+
} else {
305+
endpoint = endpoint(getSourceRequest.index(), optionalType, getSourceRequest.id(), "_source");
306+
}
307+
Request request = new Request(httpMethodName, endpoint);
315308
request.addParameters(parameters.asMap());
316309
return request;
317310
}

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,13 @@ public final Cancellable existsAsync(GetRequest getRequest, RequestOptions optio
843843
* @param getRequest the request
844844
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
845845
* @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise
846+
* @deprecated use {@link #existsSource(GetSourceRequest, RequestOptions)} instead
846847
*/
848+
@Deprecated
847849
public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException {
848-
return performRequest(getRequest, RequestConverters::sourceExists, options, RestHighLevelClient::convertExistsResponse, emptySet());
850+
GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest);
851+
return performRequest(getSourceRequest, RequestConverters::sourceExists, options,
852+
RestHighLevelClient::convertExistsResponse, emptySet());
849853
}
850854

851855
/**
@@ -856,37 +860,68 @@ public boolean existsSource(GetRequest getRequest, RequestOptions options) throw
856860
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
857861
* @param listener the listener to be notified upon request completion
858862
* @return cancellable that may be used to cancel the request
863+
* @deprecated use {@link #existsSourceAsync(GetSourceRequest, RequestOptions, ActionListener)} instead
859864
*/
865+
@Deprecated
860866
public final Cancellable existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener<Boolean> listener) {
861-
return performRequestAsync(getRequest, RequestConverters::sourceExists, options,
867+
GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest);
868+
return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options,
869+
RestHighLevelClient::convertExistsResponse, listener, emptySet());
870+
}
871+
872+
/**
873+
* Checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
874+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
875+
* on elastic.co</a>
876+
* @param getSourceRequest the request
877+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
878+
* @return <code>true</code> if the document and _source field exists, <code>false</code> otherwise
879+
*/
880+
public boolean existsSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException {
881+
return performRequest(getSourceRequest, RequestConverters::sourceExists, options,
882+
RestHighLevelClient::convertExistsResponse, emptySet());
883+
}
884+
885+
/**
886+
* Asynchronously checks for the existence of a document with a "_source" field. Returns true if it exists, false otherwise.
887+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Source exists API
888+
* on elastic.co</a>
889+
* @param getSourceRequest the request
890+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
891+
* @param listener the listener to be notified upon request completion
892+
* @return cancellable that may be used to cancel the request
893+
*/
894+
public final Cancellable existsSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options,
895+
ActionListener<Boolean> listener) {
896+
return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options,
862897
RestHighLevelClient::convertExistsResponse, listener, emptySet());
863898
}
864899

865900
/**
866901
* Retrieves the source field only of a document using GetSource API.
867902
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API
868903
* on elastic.co</a>
869-
* @param getRequest the request
904+
* @param getSourceRequest the request
870905
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
871906
* @return the response
872907
*/
873-
public GetSourceResponse getSource(GetSourceRequest getRequest, RequestOptions options) throws IOException {
874-
return performRequestAndParseEntity(getRequest, RequestConverters::getSource, options,
908+
public GetSourceResponse getSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException {
909+
return performRequestAndParseEntity(getSourceRequest, RequestConverters::getSource, options,
875910
GetSourceResponse::fromXContent, emptySet());
876911
}
877912

878913
/**
879914
* Asynchronously retrieves the source field only of a document using GetSource API.
880915
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source">Get Source API
881916
* on elastic.co</a>
882-
* @param getRequest the request
917+
* @param getSourceRequest the request
883918
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
884919
* @param listener the listener to be notified upon request completion
885920
* @return cancellable that may be used to cancel the request
886921
*/
887-
public final Cancellable getSourceAsync(GetSourceRequest getRequest, RequestOptions options,
922+
public final Cancellable getSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options,
888923
ActionListener<GetSourceResponse> listener) {
889-
return performRequestAsyncAndParseEntity(getRequest, RequestConverters::getSource, options,
924+
return performRequestAsyncAndParseEntity(getSourceRequest, RequestConverters::getSource, options,
890925
GetSourceResponse::fromXContent, listener, emptySet());
891926
}
892927

client/rest-high-level/src/main/java/org/elasticsearch/client/core/GetSourceRequest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.core;
2121

22+
import org.elasticsearch.action.get.GetRequest;
2223
import org.elasticsearch.client.Validatable;
2324
import org.elasticsearch.common.xcontent.ToXContentObject;
2425
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -36,13 +37,23 @@ public final class GetSourceRequest implements Validatable, ToXContentObject {
3637
private FetchSourceContext fetchSourceContext;
3738

3839
private String index;
40+
private String type;
3941
private String id;
4042

4143
public GetSourceRequest(String index, String id) {
4244
this.index = index;
4345
this.id = id;
4446
}
4547

48+
public static GetSourceRequest from(GetRequest getRequest) {
49+
return new GetSourceRequest(getRequest.index(), getRequest.id())
50+
.routing(getRequest.routing())
51+
.preference(getRequest.preference())
52+
.refresh(getRequest.refresh())
53+
.realtime(getRequest.realtime())
54+
.fetchSourceContext(getRequest.fetchSourceContext());
55+
}
56+
4657
/**
4758
* Controls the shard routing of the request. Using this value to hash the shard
4859
* and not the id.
@@ -100,6 +111,15 @@ public String index() {
100111
return index;
101112
}
102113

114+
public String type() {
115+
return type;
116+
}
117+
118+
public GetSourceRequest type(String type) {
119+
this.type = type;
120+
return this;
121+
}
122+
103123
public String id() {
104124
return id;
105125
}

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ public void testExists() throws IOException {
213213
}
214214
}
215215

216-
public void testSourceExists() throws IOException {
216+
// used deprecated API existsSource(GetRequest, RequestOptions)
217+
// see test `testSourceExists` with new API tests
218+
public void testDeprecatedSourceExists() throws IOException {
217219
{
218220
GetRequest getRequest = new GetRequest("index", "id");
219221
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
@@ -236,6 +238,25 @@ public void testSourceExists() throws IOException {
236238
}
237239
}
238240

241+
public void testSourceExists() throws IOException {
242+
{
243+
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
244+
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
245+
}
246+
IndexRequest index = new IndexRequest("index").id("id");
247+
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
248+
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
249+
highLevelClient().index(index, RequestOptions.DEFAULT);
250+
{
251+
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
252+
assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
253+
}
254+
{
255+
GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
256+
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
257+
}
258+
}
259+
239260
public void testSourceDoesNotExist() throws IOException {
240261
final String noSourceIndex = "no_source";
241262
{
@@ -262,7 +283,11 @@ public void testSourceDoesNotExist() throws IOException {
262283
{
263284
GetRequest getRequest = new GetRequest(noSourceIndex, "1");
264285
assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
286+
// used deprecated API existsSource(GetRequest, RequestOptions)
265287
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
288+
// used new API existsSource(GetSourceRequest, RequestOptions)
289+
GetSourceRequest getSourceRequest = new GetSourceRequest(noSourceIndex, "1");
290+
assertFalse(execute(getSourceRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
266291
}
267292
}
268293

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,22 @@ public void testGetWithType() {
165165
}
166166

167167
public void testSourceExists() throws IOException {
168-
doTestSourceExists((index, id) -> new GetRequest(index, id));
168+
doTestSourceExists((index, id) -> new GetSourceRequest(index, id));
169169
}
170170

171171
public void testSourceExistsWithType() throws IOException {
172172
String type = frequently() ? randomAlphaOfLengthBetween(3, 10) : MapperService.SINGLE_MAPPING_NAME;
173-
doTestSourceExists((index, id) -> new GetRequest(index, type, id));
173+
doTestSourceExists((index, id) -> new GetSourceRequest(index, id).type(type));
174174
}
175175

176176
public void testGetSource() throws IOException {
177177
doTestGetSource((index, id) -> new GetSourceRequest(index, id));
178178
}
179179

180-
private static void doTestSourceExists(BiFunction<String, String, GetRequest> requestFunction) throws IOException {
180+
private static void doTestSourceExists(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
181181
String index = randomAlphaOfLengthBetween(3, 10);
182182
String id = randomAlphaOfLengthBetween(3, 10);
183-
final GetRequest getRequest = requestFunction.apply(index, id);
183+
final GetSourceRequest getRequest = requestFunction.apply(index, id);
184184

185185
Map<String, String> expectedParams = new HashMap<>();
186186
if (randomBoolean()) {
@@ -210,7 +210,7 @@ private static void doTestSourceExists(BiFunction<String, String, GetRequest> re
210210
Request request = RequestConverters.sourceExists(getRequest);
211211
assertEquals(HttpHead.METHOD_NAME, request.getMethod());
212212
String type = getRequest.type();
213-
if (type.equals(MapperService.SINGLE_MAPPING_NAME)) {
213+
if (type == null) {
214214
assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
215215
} else {
216216
assertEquals("/" + index + "/" + type + "/" + id + "/_source", request.getEndpoint());

0 commit comments

Comments
 (0)