Skip to content

Commit 3869fd2

Browse files
authored
Improve delete-by-query returned information.
Original Pull Request #1692 Closes #1679
1 parent ffc2420 commit 3869fd2

14 files changed

+83
-65
lines changed

src/main/asciidoc/reference/elasticsearch-migration-guide-4.1-4.2.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ If a refresh policy is set, then it will be used by the repositories as well.
4747

4848
When configuring Spring Data Elasticsearch like described in <<elasticsearch.clients>> by using `ElasticsearchConfigurationSupport`, `AbstractElasticsearchConfiguration` or `AbstractReactiveElasticsearchConfiguration` the refresh policy will be initialized to `null`.
4949
Previously the reactive code initialized this to `IMMEDIATE`, now reactive and non-reactive code show the same behaviour.
50+
51+
=== Method return types
52+
53+
==== delete methods that take a Query
54+
55+
The reactive methods previously returned a `Mono<Long>` with the number of deleted documents, the non reactive versions were void. They now return a `Mono<ByQueryResponse>` which contains much more detailed information about the deleted documents and errors that might have occurred.

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultReactiveElasticsearchClient.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.Indices;
116116
import org.springframework.data.elasticsearch.client.util.NamedXContents;
117117
import org.springframework.data.elasticsearch.client.util.ScrollState;
118-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
118+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
119119
import org.springframework.data.util.Lazy;
120120
import org.springframework.http.HttpHeaders;
121121
import org.springframework.http.HttpMethod;
@@ -524,10 +524,10 @@ public Mono<BulkByScrollResponse> deleteBy(HttpHeaders headers, DeleteByQueryReq
524524
}
525525

526526
@Override
527-
public Mono<UpdateByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest) {
527+
public Mono<ByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest) {
528528
return sendRequest(updateRequest, requestCreator.updateByQuery(), BulkByScrollResponse.class, headers) //
529529
.next() //
530-
.map(UpdateByQueryResponse::of);
530+
.map(ByQueryResponse::of);
531531
}
532532

533533
/*

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
import org.elasticsearch.search.suggest.Suggest;
6767
import org.springframework.data.elasticsearch.client.ClientConfiguration;
6868
import org.springframework.data.elasticsearch.client.ElasticsearchHost;
69-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
69+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
7070
import org.springframework.http.HttpHeaders;
7171
import org.springframework.util.Assert;
7272
import org.springframework.util.CollectionUtils;
@@ -606,7 +606,7 @@ default Mono<BulkByScrollResponse> deleteBy(DeleteByQueryRequest deleteRequest)
606606
* * Query API on elastic.co</a>
607607
* @return a {@link Mono} emitting operation response.
608608
*/
609-
default Mono<UpdateByQueryResponse> updateBy(Consumer<UpdateByQueryRequest> consumer) {
609+
default Mono<ByQueryResponse> updateBy(Consumer<UpdateByQueryRequest> consumer) {
610610

611611
final UpdateByQueryRequest request = new UpdateByQueryRequest();
612612
consumer.accept(request);
@@ -621,7 +621,7 @@ default Mono<UpdateByQueryResponse> updateBy(Consumer<UpdateByQueryRequest> cons
621621
* * Query API on elastic.co</a>
622622
* @return a {@link Mono} emitting operation response.
623623
*/
624-
default Mono<UpdateByQueryResponse> updateBy(UpdateByQueryRequest updateRequest) {
624+
default Mono<ByQueryResponse> updateBy(UpdateByQueryRequest updateRequest) {
625625
return updateBy(HttpHeaders.EMPTY, updateRequest);
626626
}
627627

@@ -634,7 +634,7 @@ default Mono<UpdateByQueryResponse> updateBy(UpdateByQueryRequest updateRequest)
634634
* * Query API on elastic.co</a>
635635
* @return a {@link Mono} emitting operation response.
636636
*/
637-
Mono<UpdateByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest);
637+
Mono<ByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest);
638638

639639
/**
640640
* Execute a {@link BulkRequest} against the {@literal bulk} API.

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
5454
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
5555
import org.springframework.data.elasticsearch.core.query.BulkOptions;
56+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
5657
import org.springframework.data.elasticsearch.core.query.IndexQuery;
5758
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
5859
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
@@ -275,8 +276,8 @@ public String delete(String id, Class<?> entityType) {
275276
}
276277

277278
@Override
278-
public void delete(Query query, Class<?> clazz) {
279-
delete(query, clazz, getIndexCoordinatesFor(clazz));
279+
public ByQueryResponse delete(Query query, Class<?> clazz) {
280+
return delete(query, clazz, getIndexCoordinatesFor(clazz));
280281
}
281282

282283
@Override

src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
2121
import org.springframework.data.elasticsearch.core.query.BulkOptions;
22+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
2223
import org.springframework.data.elasticsearch.core.query.IndexQuery;
2324
import org.springframework.data.elasticsearch.core.query.Query;
24-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
2525
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
2626
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
2727
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
@@ -276,19 +276,21 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
276276
* @param query query defining the objects
277277
* @param clazz The entity class, must be annotated with
278278
* {@link org.springframework.data.elasticsearch.annotations.Document}
279+
* @return response with detailed information
279280
* @since 4.1
280281
*/
281-
void delete(Query query, Class<?> clazz);
282+
ByQueryResponse delete(Query query, Class<?> clazz);
282283

283284
/**
284285
* Delete all records matching the query.
285-
*
286+
*
286287
* @param query query defining the objects
287288
* @param clazz The entity class, must be annotated with
288289
* {@link org.springframework.data.elasticsearch.annotations.Document}
289290
* @param index the index from which to delete
291+
* @return response with detailed information
290292
*/
291-
void delete(Query query, Class<?> clazz, IndexCoordinates index);
293+
ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index);
292294

293295
/**
294296
* Partial update of the document.
@@ -307,5 +309,5 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
307309
* @return the update response
308310
* @since 4.2
309311
*/
310-
UpdateByQueryResponse updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
312+
ByQueryResponse updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
311313
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchRestTemplate.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
5050
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
5151
import org.springframework.data.elasticsearch.core.query.BulkOptions;
52+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
5253
import org.springframework.data.elasticsearch.core.query.IndexQuery;
5354
import org.springframework.data.elasticsearch.core.query.Query;
54-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
5555
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
5656
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
5757
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
@@ -208,9 +208,9 @@ protected String doDelete(String id, @Nullable String routing, IndexCoordinates
208208
}
209209

210210
@Override
211-
public void delete(Query query, Class<?> clazz, IndexCoordinates index) {
211+
public ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index) {
212212
DeleteByQueryRequest deleteByQueryRequest = requestFactory.deleteByQueryRequest(query, clazz, index);
213-
execute(client -> client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT));
213+
return ByQueryResponse.of(execute(client -> client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT)));
214214
}
215215

216216
@Override
@@ -231,7 +231,7 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
231231
}
232232

233233
@Override
234-
public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
234+
public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
235235

236236
Assert.notNull(query, "query must not be null");
237237
Assert.notNull(index, "index must not be null");
@@ -248,7 +248,7 @@ public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates i
248248

249249
final BulkByScrollResponse bulkByScrollResponse = execute(
250250
client -> client.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT));
251-
return UpdateByQueryResponse.of(bulkByScrollResponse);
251+
return ByQueryResponse.of(bulkByScrollResponse);
252252
}
253253

254254
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
4848
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
4949
import org.springframework.data.elasticsearch.core.query.BulkOptions;
50+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
5051
import org.springframework.data.elasticsearch.core.query.IndexQuery;
5152
import org.springframework.data.elasticsearch.core.query.Query;
52-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
5353
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
5454
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
5555
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
@@ -232,8 +232,8 @@ protected String doDelete(String id, @Nullable String routing, IndexCoordinates
232232
}
233233

234234
@Override
235-
public void delete(Query query, Class<?> clazz, IndexCoordinates index) {
236-
requestFactory.deleteByQueryRequestBuilder(client, query, clazz, index).get();
235+
public ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index) {
236+
return ByQueryResponse.of(requestFactory.deleteByQueryRequestBuilder(client, query, clazz, index).get());
237237
}
238238

239239
@Override
@@ -260,7 +260,7 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
260260
}
261261

262262
@Override
263-
public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
263+
public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
264264

265265
Assert.notNull(query, "query must not be null");
266266
Assert.notNull(index, "index must not be null");
@@ -275,7 +275,7 @@ public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates i
275275
// UpdateByQueryRequestBuilder has not parameters to set a routing value
276276

277277
final BulkByScrollResponse bulkByScrollResponse = updateByQueryRequestBuilder.execute().actionGet();
278-
return UpdateByQueryResponse.of(bulkByScrollResponse);
278+
return ByQueryResponse.of(bulkByScrollResponse);
279279
}
280280

281281
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,

src/main/java/org/springframework/data/elasticsearch/core/ReactiveDocumentOperations.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
2626
import org.springframework.data.elasticsearch.core.query.BulkOptions;
27+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
2728
import org.springframework.data.elasticsearch.core.query.Query;
28-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
2929
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
3030
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
3131
import org.springframework.util.Assert;
@@ -264,7 +264,7 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
264264
* @param entityType must not be {@literal null}.
265265
* @return a {@link Mono} emitting the number of the removed documents.
266266
*/
267-
Mono<Long> delete(Query query, Class<?> entityType);
267+
Mono<ByQueryResponse> delete(Query query, Class<?> entityType);
268268

269269
/**
270270
* Delete the documents matching the given {@link Query} extracting index from entity metadata.
@@ -274,7 +274,7 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
274274
* @param index the target index, must not be {@literal null}
275275
* @return a {@link Mono} emitting the number of the removed documents.
276276
*/
277-
Mono<Long> delete(Query query, Class<?> entityType, IndexCoordinates index);
277+
Mono<ByQueryResponse> delete(Query query, Class<?> entityType, IndexCoordinates index);
278278

279279
/**
280280
* Partial update of the document.
@@ -294,5 +294,5 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
294294
* @return a {@link Mono} emitting the update response
295295
* @since 4.2
296296
*/
297-
Mono<UpdateByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
297+
Mono<ByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
298298
}

src/main/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplate.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import org.springframework.data.elasticsearch.core.query.IndexQuery;
7575
import org.springframework.data.elasticsearch.core.query.Query;
7676
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
77-
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
77+
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
7878
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
7979
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
8080
import org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver;
@@ -526,11 +526,11 @@ private Mono<String> doDeleteById(String id, @Nullable String routing, IndexCoor
526526
* @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#delete(Query, Class, IndexCoordinates)
527527
*/
528528
@Override
529-
public Mono<Long> delete(Query query, Class<?> entityType, IndexCoordinates index) {
529+
public Mono<ByQueryResponse> delete(Query query, Class<?> entityType, IndexCoordinates index) {
530530

531531
Assert.notNull(query, "Query must not be null!");
532532

533-
return doDeleteBy(query, entityType, index).map(BulkByScrollResponse::getDeleted).next();
533+
return doDeleteBy(query, entityType, index).map(ByQueryResponse::of);
534534
}
535535

536536
@Override
@@ -556,7 +556,7 @@ public Mono<UpdateResponse> update(UpdateQuery updateQuery, IndexCoordinates ind
556556
}
557557

558558
@Override
559-
public Mono<UpdateByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index) {
559+
public Mono<ByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index) {
560560

561561
Assert.notNull(updateQuery, "updateQuery must not be null");
562562
Assert.notNull(index, "Index must not be null");
@@ -578,13 +578,13 @@ public Mono<UpdateByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexC
578578
}
579579

580580
@Override
581-
public Mono<Long> delete(Query query, Class<?> entityType) {
581+
public Mono<ByQueryResponse> delete(Query query, Class<?> entityType) {
582582
return delete(query, entityType, getIndexCoordinatesFor(entityType));
583583
}
584584

585-
private Flux<BulkByScrollResponse> doDeleteBy(Query query, Class<?> entityType, IndexCoordinates index) {
585+
private Mono<BulkByScrollResponse> doDeleteBy(Query query, Class<?> entityType, IndexCoordinates index) {
586586

587-
return Flux.defer(() -> {
587+
return Mono.defer(() -> {
588588
DeleteByQueryRequest request = requestFactory.deleteByQueryRequest(query, entityType, index);
589589
return doDeleteBy(prepareDeleteByRequest(request));
590590
});

0 commit comments

Comments
 (0)