diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java index b6b57f96a..5197a317f 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java @@ -139,7 +139,7 @@ private ClusterTemplate getClusterTemplate() { public T get(String id, Class clazz, IndexCoordinates index) { GetRequest getRequest = requestConverter.documentGetRequest(elasticsearchConverter.convertId(id), - routingResolver.getRouting(), index, false); + routingResolver.getRouting(), index); GetResponse getResponse = execute(client -> client.get(getRequest, EntityAsMap.class)); ReadDocumentCallback callback = new ReadDocumentCallback<>(elasticsearchConverter, clazz, index); @@ -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 diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchClient.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchClient.java index 39131d78d..5f65f2549 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchClient.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchClient.java @@ -128,6 +128,13 @@ public Mono> get(GetRequest request, Class tClass) { return Mono.fromFuture(transport.performRequestAsync(request, endpoint, transportOptions)); } + public Mono exists(ExistsRequest request) { + + Assert.notNull(request, "request must not be null"); + + return Mono.fromFuture(transport.performRequestAsync(request, ExistsRequest._ENDPOINT, transportOptions)); + } + public Mono> update(UpdateRequest request, Class clazz) { Assert.notNull(request, "request must not be null"); diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java index 1fdb0b116..facdf0287 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java @@ -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; @@ -147,11 +148,11 @@ protected Mono 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>>) client -> client.get(getRequest, EntityAsMap.class)))) - .map(GetResult::found) // + ((ClientCallback>) client -> client.exists(existsRequest)))) + .map(BooleanResponse::value) // .onErrorReturn(NoSuchIndexException.class, false); } @@ -174,7 +175,7 @@ public Mono get(String id, Class 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 = Mono.from(execute( (ClientCallback>>) client -> client.get(getRequest, EntityAsMap.class))); diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java index 4778bd594..6151361a2 100644 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java @@ -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 MgetRequest documentMgetRequest(Query query, Class clazz, IndexCoordinates index) {