Skip to content

Commit 2f651c9

Browse files
committed
Add count methods in ELC ReactiveElasticsearchClient.
Add count methods using CountRequest with just ELC's ReactiveElasticsearchClient, without having to initialize any entity or repository. Also, refactored existing doCount method from ELC's ReactiveElasticsearchTemplate to use the newly created count method. Closes spring-projects#2749
1 parent 98716a8 commit 2f651c9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchClient.java

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Reactive version of {@link co.elastic.clients.elasticsearch.ElasticsearchClient}.
3737
*
3838
* @author Peter-Josef Meisch
39+
* @author maryantocinn
3940
* @since 4.4
4041
*/
4142
public class ReactiveElasticsearchClient extends ApiClient<ElasticsearchTransport, ReactiveElasticsearchClient>
@@ -227,6 +228,20 @@ public Mono<DeleteByQueryResponse> deleteByQuery(
227228
return deleteByQuery(fn.apply(new DeleteByQueryRequest.Builder()).build());
228229
}
229230

231+
public Mono<CountResponse> count(CountRequest request) {
232+
233+
Assert.notNull(request, "request must not be null");
234+
235+
return Mono.fromFuture(transport.performRequestAsync(request, CountRequest._ENDPOINT, transportOptions));
236+
}
237+
238+
public Mono<CountResponse> count(Function<CountRequest.Builder, ObjectBuilder<CountRequest>> fn) {
239+
240+
Assert.notNull(fn, "fn must not be null");
241+
242+
return count(fn.apply(new CountRequest.Builder()).build());
243+
}
244+
230245
// endregion
231246
// region search
232247

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchTemplate.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* @author Peter-Josef Meisch
7676
* @author Illia Ulianov
7777
* @author Junghoon Ban
78+
* @author maryantocinn
7879
* @since 4.4
7980
*/
8081
public class ReactiveElasticsearchTemplate extends AbstractReactiveElasticsearchTemplate {
@@ -483,11 +484,10 @@ protected Mono<Long> doCount(Query query, Class<?> entityType, IndexCoordinates
483484
Assert.notNull(query, "query must not be null");
484485
Assert.notNull(index, "index must not be null");
485486

486-
SearchRequest searchRequest = requestConverter.searchRequest(query, routingResolver.getRouting(), entityType, index,
487-
true);
487+
CountRequest request = requestConverter.countRequest(query, entityType, index);
488488

489-
return Mono.from(execute(client -> client.search(searchRequest, EntityAsMap.class)))
490-
.map(searchResponse -> searchResponse.hits().total() != null ? searchResponse.hits().total().value() : 0L);
489+
return Mono.from(execute(client -> client.count(request))) //
490+
.map(CountResponse::count);
491491
}
492492

493493
private Flux<SearchDocument> doFindBounded(Query query, Class<?> clazz, IndexCoordinates index) {

src/main/java/org/springframework/data/elasticsearch/client/elc/RequestConverter.java

+13
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
* @author cdalxndr
112112
* @author scoobyzhang
113113
* @author Haibo Liu
114+
* @author maryantocinn
114115
* @since 4.4
115116
*/
116117
class RequestConverter extends AbstractQueryProcessor {
@@ -1188,6 +1189,18 @@ public UpdateByQueryRequest documentUpdateByQueryRequest(UpdateQuery updateQuery
11881189
});
11891190
}
11901191

1192+
public <T> CountRequest countRequest(Query query, @Nullable Class<T> clazz, IndexCoordinates indexCoordinates) {
1193+
1194+
Assert.notNull(query, "query must not be null");
1195+
Assert.notNull(indexCoordinates, "index must not be null");
1196+
1197+
elasticsearchConverter.updateQuery(query, clazz);
1198+
1199+
return CountRequest.of(b -> b //
1200+
.query(getQuery(query, clazz)) //
1201+
.index(Arrays.asList(indexCoordinates.getIndexNames())));
1202+
}
1203+
11911204
// endregion
11921205

11931206
// region search

0 commit comments

Comments
 (0)