Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use ExistsRequest replace GetRequest to check documents exists.
  • Loading branch information
mawen12 committed Oct 12, 2023
1 parent 406961c commit 0a17e2a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,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 @@ -235,9 +235,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 reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
Expand Down Expand Up @@ -147,11 +148,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(
((ClientCallback<Publisher<GetResponse<EntityAsMap>>>) client -> client.get(getRequest, EntityAsMap.class))))
.map(GetResult::found) //
((ClientCallback<Publisher<BooleanResponse>>) client -> client.exists(existsRequest))))
.map(BooleanResponse::value) //
.onErrorReturn(NoSuchIndexException.class, false);
}

Expand All @@ -174,7 +175,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(
(ClientCallback<Publisher<GetResponse<EntityAsMap>>>) client -> client.get(getRequest, EntityAsMap.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,25 +763,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

0 comments on commit 0a17e2a

Please sign in to comment.