Skip to content

Commit 7998d89

Browse files
committed
Support Delete by query with es parameters.
1 parent aa27bbe commit 7998d89

File tree

7 files changed

+616
-0
lines changed

7 files changed

+616
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder;
5555
import org.springframework.data.elasticsearch.core.query.BulkOptions;
5656
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
57+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
5758
import org.springframework.data.elasticsearch.core.query.IndexQuery;
5859
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
5960
import org.springframework.data.elasticsearch.core.query.Query;
@@ -177,6 +178,11 @@ public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, Index
177178
doBulkOperation(queries, bulkOptions, index);
178179
}
179180

181+
@Override
182+
public ByQueryResponse delete(DeleteQuery query, Class<?> clazz) {
183+
return delete(query, clazz, getIndexCoordinatesFor(clazz));
184+
}
185+
180186
@Override
181187
public ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index) {
182188

@@ -190,6 +196,18 @@ public ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates inde
190196
return responseConverter.byQueryResponse(response);
191197
}
192198

199+
@Override
200+
public ByQueryResponse delete(DeleteQuery query, Class<?> clazz, IndexCoordinates index) {
201+
Assert.notNull(query, "query must not be null");
202+
203+
DeleteByQueryRequest request = requestConverter.documentDeleteByQueryRequest(query, routingResolver.getRouting(),
204+
clazz, index, getRefreshPolicy());
205+
206+
DeleteByQueryResponse response = execute(client -> client.deleteByQuery(request));
207+
208+
return responseConverter.byQueryResponse(response);
209+
}
210+
193211
@Override
194212
public UpdateResponse update(UpdateQuery updateQuery, IndexCoordinates index) {
195213

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import co.elastic.clients.json.JsonpMapper;
2626
import co.elastic.clients.transport.Version;
2727
import co.elastic.clients.transport.endpoints.BooleanResponse;
28+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
2829
import reactor.core.publisher.Flux;
2930
import reactor.core.publisher.Mono;
3031
import reactor.util.function.Tuple2;
@@ -180,6 +181,15 @@ public Mono<ByQueryResponse> delete(Query query, Class<?> entityType, IndexCoord
180181
return Mono.from(execute(client -> client.deleteByQuery(request))).map(responseConverter::byQueryResponse);
181182
}
182183

184+
@Override
185+
public Mono<ByQueryResponse> delete(DeleteQuery query, Class<?> entityType, IndexCoordinates index) {
186+
Assert.notNull(query, "query must not be null");
187+
188+
DeleteByQueryRequest request = requestConverter.documentDeleteByQueryRequest(query, routingResolver.getRouting(),
189+
entityType, index, getRefreshPolicy());
190+
return Mono.from(execute(client -> client.deleteByQuery(request))).map(responseConverter::byQueryResponse);
191+
}
192+
183193
@Override
184194
public <T> Mono<T> get(String id, Class<T> entityType, IndexCoordinates index) {
185195

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,60 @@ public DeleteByQueryRequest documentDeleteByQueryRequest(Query query, @Nullable
968968
});
969969
}
970970

971+
public DeleteByQueryRequest documentDeleteByQueryRequest(DeleteQuery query, @Nullable String routing, Class<?> clazz,
972+
IndexCoordinates index, @Nullable RefreshPolicy refreshPolicy) {
973+
Assert.notNull(query, "query must not be null");
974+
Assert.notNull(index, "index must not be null");
975+
976+
// Old documentDeleteByQueryRequest needs to be refactored.
977+
return DeleteByQueryRequest.of(dqb -> {
978+
dqb.index(Arrays.asList(index.getIndexNames())) //
979+
.query(getQuery(query, clazz))//
980+
.refresh(deleteByQueryRefresh(refreshPolicy));
981+
982+
if (query.isLimiting()) {
983+
// noinspection ConstantConditions
984+
dqb.maxDocs(Long.valueOf(query.getMaxResults()));
985+
}
986+
987+
dqb.scroll(time(query.getScrollTime()))
988+
.scrollSize(query.getScrollSize());
989+
990+
if (query.getRoute() != null) {
991+
dqb.routing(query.getRoute());
992+
} else if (StringUtils.hasText(routing)) {
993+
dqb.routing(routing);
994+
}
995+
996+
if (query.getQ() != null) {
997+
dqb.q(query.getQ())
998+
.analyzer(query.getAnalyzer())
999+
.analyzeWildcard(query.getAnalyzeWildcard())
1000+
.defaultOperator(query.getDefaultOperator())
1001+
.df(query.getDf())
1002+
.lenient(query.getLenient());
1003+
}
1004+
1005+
if (query.getExpandWildcards() != null && !query.getExpandWildcards().isEmpty()) {
1006+
dqb.expandWildcards(expandWildcards(query.getExpandWildcards()));
1007+
}
1008+
dqb.allowNoIndices(query.getAllowNoIndices())
1009+
.conflicts(query.getConflicts())
1010+
.ignoreUnavailable(query.getIgnoreUnavailable())
1011+
.preference(query.getPreference())
1012+
.requestCache(query.getRequestCache())
1013+
.searchType(searchType(query.getSearchType()))
1014+
.searchTimeout(time(query.getSearchTimeout()))
1015+
.slices(query.getSlices())
1016+
.stats(query.getStats())
1017+
.terminateAfter(query.getTerminateAfter())
1018+
.timeout(time(query.getTimeout()))
1019+
.version(query.getVersion());
1020+
1021+
return dqb;
1022+
});
1023+
}
1024+
9711025
public UpdateRequest<Document, ?> documentUpdateRequest(UpdateQuery query, IndexCoordinates index,
9721026
@Nullable RefreshPolicy refreshPolicy, @Nullable String routing) {
9731027

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
1819
import reactor.core.publisher.Flux;
1920
import reactor.core.publisher.Mono;
2021
import reactor.core.publisher.Sinks;
@@ -412,6 +413,11 @@ public Mono<String> delete(String id, IndexCoordinates index) {
412413
public Mono<ByQueryResponse> delete(Query query, Class<?> entityType) {
413414
return delete(query, entityType, getIndexCoordinatesFor(entityType));
414415
}
416+
417+
@Override
418+
public Mono<ByQueryResponse> delete(DeleteQuery query, Class<?> entityType) {
419+
return delete(query, entityType, getIndexCoordinatesFor(entityType));
420+
}
415421
// endregion
416422

417423
// region SearchDocument

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
2222
import org.springframework.data.elasticsearch.core.query.BulkOptions;
2323
import org.springframework.data.elasticsearch.core.query.ByQueryResponse;
24+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
2425
import org.springframework.data.elasticsearch.core.query.IndexQuery;
2526
import org.springframework.data.elasticsearch.core.query.Query;
2627
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
@@ -279,9 +280,21 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
279280
* {@link org.springframework.data.elasticsearch.annotations.Document}
280281
* @return response with detailed information
281282
* @since 4.1
283+
* @deprecated since 5.3.0, use {@link #delete(DeleteQuery, Class)}
282284
*/
283285
ByQueryResponse delete(Query query, Class<?> clazz);
284286

287+
/**
288+
* Delete all records matching the query.
289+
*
290+
* @param query query defining the objects
291+
* @param clazz The entity class must be annotated with
292+
* {@link org.springframework.data.elasticsearch.annotations.Document}
293+
* @return response with detailed information
294+
* @since 5.3
295+
*/
296+
ByQueryResponse delete(DeleteQuery query, Class<?> clazz);
297+
285298
/**
286299
* Delete all records matching the query.
287300
*
@@ -290,9 +303,22 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
290303
* {@link org.springframework.data.elasticsearch.annotations.Document}
291304
* @param index the index from which to delete
292305
* @return response with detailed information
306+
* @deprecated since 5.3.0, use {@link #delete(DeleteQuery, Class, IndexCoordinates)}
293307
*/
294308
ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index);
295309

310+
/**
311+
* Delete all records matching the query.
312+
*
313+
* @param query query defining the objects
314+
* @param clazz The entity class must be annotated with
315+
* {@link org.springframework.data.elasticsearch.annotations.Document}
316+
* @param index the index from which to delete
317+
* @return response with detailed information
318+
* @since 5.3
319+
*/
320+
ByQueryResponse delete(DeleteQuery query, Class<?> clazz, IndexCoordinates index);
321+
296322
/**
297323
* Partially update a document by the given entity.
298324
*

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
1819
import reactor.core.publisher.Flux;
1920
import reactor.core.publisher.Mono;
2021

@@ -331,19 +332,42 @@ default Mono<Void> bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index)
331332
* @param query must not be {@literal null}.
332333
* @param entityType must not be {@literal null}.
333334
* @return a {@link Mono} emitting the number of the removed documents.
335+
* @deprecated since 5.3.0, use {@link #delete(DeleteQuery, Class)}
334336
*/
335337
Mono<ByQueryResponse> delete(Query query, Class<?> entityType);
336338

339+
/**
340+
* Delete the documents matching the given {@link Query} extracting index from entity metadata.
341+
*
342+
* @param query must not be {@literal null}.
343+
* @param entityType must not be {@literal null}.
344+
* @return a {@link Mono} emitting the number of the removed documents.
345+
* @since 5.3
346+
*/
347+
Mono<ByQueryResponse> delete(DeleteQuery query, Class<?> entityType);
348+
337349
/**
338350
* Delete the documents matching the given {@link Query} extracting index from entity metadata.
339351
*
340352
* @param query must not be {@literal null}.
341353
* @param entityType must not be {@literal null}.
342354
* @param index the target index, must not be {@literal null}
343355
* @return a {@link Mono} emitting the number of the removed documents.
356+
* @deprecated since 5.3.0, use {@link #delete(DeleteQuery, Class, IndexCoordinates)}
344357
*/
345358
Mono<ByQueryResponse> delete(Query query, Class<?> entityType, IndexCoordinates index);
346359

360+
/**
361+
* Delete the documents matching the given {@link Query} extracting index from entity metadata.
362+
*
363+
* @param query must not be {@literal null}.
364+
* @param entityType must not be {@literal null}.
365+
* @param index the target index, must not be {@literal null}
366+
* @return a {@link Mono} emitting the number of the removed documents.
367+
* @since 5.3
368+
*/
369+
Mono<ByQueryResponse> delete(DeleteQuery query, Class<?> entityType, IndexCoordinates index);
370+
347371
/**
348372
* Partial update of the document.
349373
*

0 commit comments

Comments
 (0)