Skip to content

Use ExistsRequest replace GetRequest to check documents exists. #2728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private ClusterTemplate getClusterTemplate() {
public <T> T get(String id, Class<T> clazz, IndexCoordinates index) {

GetRequest getRequest = requestConverter.documentGetRequest(elasticsearchConverter.convertId(id),
routingResolver.getRouting(), index, false);
routingResolver.getRouting(), index);
GetResponse<EntityAsMap> getResponse = execute(client -> client.get(getRequest, EntityAsMap.class));

ReadDocumentCallback<T> callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index);
Expand Down Expand Up @@ -221,9 +221,9 @@ protected boolean doExists(String id, IndexCoordinates index) {
Assert.notNull(id, "id must not be null");
Assert.notNull(index, "index must not be null");

GetRequest request = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, true);
ExistsRequest request = requestConverter.documentExistsRequest(id, routingResolver.getRouting(), index);

return execute(client -> client.get(request, EntityAsMap.class)).found();
return execute(client -> client.exists(request)).value();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public <T> Mono<GetResponse<T>> get(GetRequest request, Class<T> tClass) {
return Mono.fromFuture(transport.performRequestAsync(request, endpoint, transportOptions));
}

public Mono<BooleanResponse> exists(ExistsRequest request) {

Assert.notNull(request, "request must not be null");

return Mono.fromFuture(transport.performRequestAsync(request, ExistsRequest._ENDPOINT, transportOptions));
}

public <T, P> Mono<UpdateResponse<T>> update(UpdateRequest<T, P> request, Class<T> clazz) {

Assert.notNull(request, "request must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import co.elastic.clients.elasticsearch.core.search.ResponseBody;
import co.elastic.clients.json.JsonpMapper;
import co.elastic.clients.transport.Version;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -148,9 +149,11 @@ protected Mono<Boolean> doExists(String id, IndexCoordinates index) {
Assert.notNull(id, "id must not be null");
Assert.notNull(index, "index must not be null");

GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, true);
ExistsRequest existsRequest = requestConverter.documentExistsRequest(id, routingResolver.getRouting(), index);

return Mono.from(execute(client -> client.get(getRequest, EntityAsMap.class))).map(GetResult::found) //
return Mono.from(execute(
((ClientCallback<Publisher<BooleanResponse>>) client -> client.exists(existsRequest))))
.map(BooleanResponse::value) //
.onErrorReturn(NoSuchIndexException.class, false);
}

Expand All @@ -171,7 +174,7 @@ public <T> Mono<T> get(String id, Class<T> entityType, IndexCoordinates index) {
Assert.notNull(entityType, "entityType must not be null");
Assert.notNull(index, "index must not be null");

GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index, false);
GetRequest getRequest = requestConverter.documentGetRequest(id, routingResolver.getRouting(), index);

Mono<GetResponse<EntityAsMap>> getResponse = Mono
.from(execute(client -> client.get(getRequest, EntityAsMap.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,25 +780,26 @@ public BulkRequest documentBulkRequest(List<?> queries, BulkOptions bulkOptions,
return builder.build();
}

public GetRequest documentGetRequest(String id, @Nullable String routing, IndexCoordinates indexCoordinates,
boolean forExistsRequest) {
public GetRequest documentGetRequest(String id, @Nullable String routing, IndexCoordinates indexCoordinates) {

Assert.notNull(id, "id must not be null");
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");

return GetRequest.of(grb -> {
grb //
return GetRequest.of(grb -> grb //
.index(indexCoordinates.getIndexName()) //
.id(id) //
.routing(routing);
.routing(routing));
}

if (forExistsRequest) {
grb.source(scp -> scp.fetch(false));
}
public co.elastic.clients.elasticsearch.core.ExistsRequest documentExistsRequest(String id, @Nullable String routing, IndexCoordinates indexCoordinates) {

return grb;
});
Assert.notNull(id, "id must not be null");
Assert.notNull(indexCoordinates, "indexCoordinates must not be null");

return co.elastic.clients.elasticsearch.core.ExistsRequest.of(erb -> erb
.index(indexCoordinates.getIndexName())
.id(id)
.routing(routing));
}

public <T> MgetRequest documentMgetRequest(Query query, Class<T> clazz, IndexCoordinates index) {
Expand Down