Skip to content

Improve delete-by-query returned information. #1692

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
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 @@ -47,3 +47,9 @@ If a refresh policy is set, then it will be used by the repositories as well.

When configuring Spring Data Elasticsearch like described in <<elasticsearch.clients>> by using `ElasticsearchConfigurationSupport`, `AbstractElasticsearchConfiguration` or `AbstractReactiveElasticsearchConfiguration` the refresh policy will be initialized to `null`.
Previously the reactive code initialized this to `IMMEDIATE`, now reactive and non-reactive code show the same behaviour.

=== Method return types

==== delete methods that take a Query

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.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient.Indices;
import org.springframework.data.elasticsearch.client.util.NamedXContents;
import org.springframework.data.elasticsearch.client.util.ScrollState;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.util.Lazy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -524,10 +524,10 @@ public Mono<BulkByScrollResponse> deleteBy(HttpHeaders headers, DeleteByQueryReq
}

@Override
public Mono<UpdateByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest) {
public Mono<ByQueryResponse> updateBy(HttpHeaders headers, UpdateByQueryRequest updateRequest) {
return sendRequest(updateRequest, requestCreator.updateByQuery(), BulkByScrollResponse.class, headers) //
.next() //
.map(UpdateByQueryResponse::of);
.map(ByQueryResponse::of);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import org.elasticsearch.search.suggest.Suggest;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.ElasticsearchHost;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -606,7 +606,7 @@ default Mono<BulkByScrollResponse> deleteBy(DeleteByQueryRequest deleteRequest)
* * Query API on elastic.co</a>
* @return a {@link Mono} emitting operation response.
*/
default Mono<UpdateByQueryResponse> updateBy(Consumer<UpdateByQueryRequest> consumer) {
default Mono<ByQueryResponse> updateBy(Consumer<UpdateByQueryRequest> consumer) {

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

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

/**
* Execute a {@link BulkRequest} against the {@literal bulk} API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
Expand Down Expand Up @@ -275,8 +276,8 @@ public String delete(String id, Class<?> entityType) {
}

@Override
public void delete(Query query, Class<?> clazz) {
delete(query, clazz, getIndexCoordinatesFor(clazz));
public ByQueryResponse delete(Query query, Class<?> clazz) {
return delete(query, clazz, getIndexCoordinatesFor(clazz));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
Expand Down Expand Up @@ -276,19 +276,21 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
* @param query query defining the objects
* @param clazz The entity class, must be annotated with
* {@link org.springframework.data.elasticsearch.annotations.Document}
* @return response with detailed information
* @since 4.1
*/
void delete(Query query, Class<?> clazz);
ByQueryResponse delete(Query query, Class<?> clazz);

/**
* Delete all records matching the query.
*
*
* @param query query defining the objects
* @param clazz The entity class, must be annotated with
* {@link org.springframework.data.elasticsearch.annotations.Document}
* @param index the index from which to delete
* @return response with detailed information
*/
void delete(Query query, Class<?> clazz, IndexCoordinates index);
ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index);

/**
* Partial update of the document.
Expand All @@ -307,5 +309,5 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
* @return the update response
* @since 4.2
*/
UpdateByQueryResponse updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
ByQueryResponse updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
Expand Down Expand Up @@ -208,9 +208,9 @@ protected String doDelete(String id, @Nullable String routing, IndexCoordinates
}

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

@Override
Expand All @@ -231,7 +231,7 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
}

@Override
public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {

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

final BulkByScrollResponse bulkByScrollResponse = execute(
client -> client.updateByQuery(updateByQueryRequest, RequestOptions.DEFAULT));
return UpdateByQueryResponse.of(bulkByScrollResponse);
return ByQueryResponse.of(bulkByScrollResponse);
}

public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.data.elasticsearch.support.SearchHitsUtil;
Expand Down Expand Up @@ -232,8 +232,8 @@ protected String doDelete(String id, @Nullable String routing, IndexCoordinates
}

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

@Override
Expand All @@ -260,7 +260,7 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
}

@Override
public UpdateByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {
public ByQueryResponse updateByQuery(UpdateQuery query, IndexCoordinates index) {

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

final BulkByScrollResponse bulkByScrollResponse = updateByQueryRequestBuilder.execute().actionGet();
return UpdateByQueryResponse.of(bulkByScrollResponse);
return ByQueryResponse.of(bulkByScrollResponse);
}

public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.BulkOptions;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -264,7 +264,7 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
* @param entityType must not be {@literal null}.
* @return a {@link Mono} emitting the number of the removed documents.
*/
Mono<Long> delete(Query query, Class<?> entityType);
Mono<ByQueryResponse> delete(Query query, Class<?> entityType);

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

/**
* Partial update of the document.
Expand All @@ -294,5 +294,5 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
* @return a {@link Mono} emitting the update response
* @since 4.2
*/
Mono<UpdateByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
Mono<ByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
import org.springframework.data.elasticsearch.core.query.UpdateByQueryResponse;
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver;
Expand Down Expand Up @@ -526,11 +526,11 @@ private Mono<String> doDeleteById(String id, @Nullable String routing, IndexCoor
* @see org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations#delete(Query, Class, IndexCoordinates)
*/
@Override
public Mono<Long> delete(Query query, Class<?> entityType, IndexCoordinates index) {
public Mono<ByQueryResponse> delete(Query query, Class<?> entityType, IndexCoordinates index) {

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

return doDeleteBy(query, entityType, index).map(BulkByScrollResponse::getDeleted).next();
return doDeleteBy(query, entityType, index).map(ByQueryResponse::of);
}

@Override
Expand All @@ -556,7 +556,7 @@ public Mono<UpdateResponse> update(UpdateQuery updateQuery, IndexCoordinates ind
}

@Override
public Mono<UpdateByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index) {
public Mono<ByQueryResponse> updateByQuery(UpdateQuery updateQuery, IndexCoordinates index) {

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

@Override
public Mono<Long> delete(Query query, Class<?> entityType) {
public Mono<ByQueryResponse> delete(Query query, Class<?> entityType) {
return delete(query, entityType, getIndexCoordinatesFor(entityType));
}

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

return Flux.defer(() -> {
return Mono.defer(() -> {
DeleteByQueryRequest request = requestFactory.deleteByQueryRequest(query, entityType, index);
return doDeleteBy(prepareDeleteByRequest(request));
});
Expand Down
Loading