Skip to content

Commit 44dc8cd

Browse files
committed
SourceExists HLRC uses GetSourceRequest instead of GetRequest
Ref elastic#50885
1 parent 92be385 commit 44dc8cd

File tree

5 files changed

+91
-32
lines changed

5 files changed

+91
-32
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,22 +275,15 @@ private static Request getStyleRequest(String method, GetRequest getRequest) {
275275
return request;
276276
}
277277

278-
static Request sourceExists(GetRequest getRequest) {
279-
Params parameters = new Params();
280-
parameters.withPreference(getRequest.preference());
281-
parameters.withRouting(getRequest.routing());
282-
parameters.withRefresh(getRequest.refresh());
283-
parameters.withRealtime(getRequest.realtime());
284-
parameters.withFetchSourceContext(getRequest.fetchSourceContext());
285-
// Version params are not currently supported by the _source API so are not passed
286-
287-
String endpoint = endpoint(getRequest.index(), "_source", getRequest.id());
288-
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
289-
request.addParameters(parameters.asMap());
290-
return request;
278+
static Request sourceExists(GetSourceRequest getSourceRequest) {
279+
return sourceRequest(getSourceRequest, HttpHead.METHOD_NAME);
291280
}
292281

293282
static Request getSource(GetSourceRequest getSourceRequest) {
283+
return sourceRequest(getSourceRequest, HttpGet.METHOD_NAME);
284+
}
285+
286+
private static Request sourceRequest(GetSourceRequest getSourceRequest, String httpMethodName) {
294287
Params parameters = new Params();
295288
parameters.withPreference(getSourceRequest.preference());
296289
parameters.withRouting(getSourceRequest.routing());
@@ -299,7 +292,7 @@ static Request getSource(GetSourceRequest getSourceRequest) {
299292
parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext());
300293

301294
String endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
302-
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
295+
Request request = new Request(httpMethodName, endpoint);
303296
request.addParameters(parameters.asMap());
304297
return request;
305298
}

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: 10 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;
@@ -43,6 +44,15 @@ public GetSourceRequest(String index, String id) {
4344
this.id = id;
4445
}
4546

47+
public static GetSourceRequest from(GetRequest getRequest) {
48+
return new GetSourceRequest(getRequest.index(), getRequest.id())
49+
.routing(getRequest.routing())
50+
.preference(getRequest.preference())
51+
.refresh(getRequest.refresh())
52+
.realtime(getRequest.realtime())
53+
.fetchSourceContext(getRequest.fetchSourceContext());
54+
}
55+
4656
/**
4757
* Controls the shard routing of the request. Using this value to hash the shard
4858
* and not the id.

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
@@ -181,7 +181,9 @@ public void testExists() throws IOException {
181181
}
182182
}
183183

184-
public void testSourceExists() throws IOException {
184+
// used deprecated API existsSource(GetRequest, RequestOptions)
185+
// see test `testSourceExists` with new API tests
186+
public void testDeprecatedSourceExists() throws IOException {
185187
{
186188
GetRequest getRequest = new GetRequest("index", "id");
187189
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
@@ -204,6 +206,25 @@ public void testSourceExists() throws IOException {
204206
}
205207
}
206208

209+
public void testSourceExists() throws IOException {
210+
{
211+
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
212+
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
213+
}
214+
IndexRequest index = new IndexRequest("index").id("id");
215+
index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
216+
index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
217+
highLevelClient().index(index, RequestOptions.DEFAULT);
218+
{
219+
GetSourceRequest getRequest = new GetSourceRequest("index", "id");
220+
assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
221+
}
222+
{
223+
GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
224+
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
225+
}
226+
}
227+
207228
public void testSourceDoesNotExist() throws IOException {
208229
final String noSourceIndex = "no_source";
209230
{
@@ -230,7 +251,11 @@ public void testSourceDoesNotExist() throws IOException {
230251
{
231252
GetRequest getRequest = new GetRequest(noSourceIndex, "1");
232253
assertTrue(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync));
254+
// used deprecated API existsSource(GetRequest, RequestOptions)
233255
assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
256+
// used new API existsSource(GetSourceRequest, RequestOptions)
257+
GetSourceRequest getSourceRequest = new GetSourceRequest(noSourceIndex, "1");
258+
assertFalse(execute(getSourceRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
234259
}
235260
}
236261

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,17 @@ public void testGet() {
154154
}
155155

156156
public void testSourceExists() throws IOException {
157-
doTestSourceExists((index, id) -> new GetRequest(index, id));
158-
}
159-
160-
public void testSourceExistsWithType() throws IOException {
161-
doTestSourceExists((index, id) -> new GetRequest(index, id));
157+
doTestSourceExists((index, id) -> new GetSourceRequest(index, id));
162158
}
163159

164160
public void testGetSource() throws IOException {
165161
doTestGetSource((index, id) -> new GetSourceRequest(index, id));
166162
}
167163

168-
private static void doTestSourceExists(BiFunction<String, String, GetRequest> requestFunction) throws IOException {
164+
private static void doTestSourceExists(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
169165
String index = randomAlphaOfLengthBetween(3, 10);
170166
String id = randomAlphaOfLengthBetween(3, 10);
171-
final GetRequest getRequest = requestFunction.apply(index, id);
167+
final GetSourceRequest getRequest = requestFunction.apply(index, id);
172168

173169
Map<String, String> expectedParams = new HashMap<>();
174170
if (randomBoolean()) {
@@ -184,7 +180,7 @@ private static void doTestSourceExists(BiFunction<String, String, GetRequest> re
184180
if (randomBoolean()) {
185181
boolean realtime = randomBoolean();
186182
getRequest.realtime(realtime);
187-
if (realtime == false) {
183+
if (!realtime) {
188184
expectedParams.put("realtime", "false");
189185
}
190186
}
@@ -222,7 +218,7 @@ private static void doTestGetSource(BiFunction<String, String, GetSourceRequest>
222218
if (randomBoolean()) {
223219
boolean realtime = randomBoolean();
224220
getRequest.realtime(realtime);
225-
if (realtime == false) {
221+
if (!realtime) {
226222
expectedParams.put("realtime", "false");
227223
}
228224
}

0 commit comments

Comments
 (0)