Skip to content

Commit 9abcacb

Browse files
authored
Use routing info on delete operations.
Original Pull Request #2755 Closes #2754
1 parent 3b93b6a commit 9abcacb

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ public ElasticsearchPersistentEntity<?> getPersistentEntityFor(@Nullable Class<?
758758

759759
public abstract Mono<String> getClusterVersion();
760760

761+
@Nullable
762+
public String getEntityRouting(Object entity) {
763+
return entityOperations.forEntity(entity, converter.getConversionService(), routingResolver)
764+
.getRouting();
765+
}
766+
761767
/**
762768
* Value class to capture client independent information from a response to an index request.
763769
*/

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

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ public interface ReactiveElasticsearchOperations
7777
*/
7878
ReactiveClusterOperations cluster();
7979

80+
/**
81+
* gets the routing for an entity.
82+
*
83+
* @param entity the entity
84+
* @return the routing, may be null if not set.
85+
* @since 5.2
86+
*/
87+
@Nullable
88+
String getEntityRouting(Object entity);
89+
8090
// region customizations
8191
/**
8292
* Returns a copy of this instance with the same configuration, but that uses a different {@link RoutingResolver} to

src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.elasticsearch.core.query.BaseQuery;
4141
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
4242
import org.springframework.data.elasticsearch.core.query.Query;
43+
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
4344
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
4445
import org.springframework.data.util.StreamUtils;
4546
import org.springframework.data.util.Streamable;
@@ -257,31 +258,31 @@ public void deleteById(ID id) {
257258

258259
Assert.notNull(id, "Cannot delete entity with id 'null'.");
259260

260-
doDelete(id, getIndexCoordinates());
261+
doDelete(id, null, getIndexCoordinates());
261262
}
262263

263264
@Override
264265
public void deleteById(ID id, @Nullable RefreshPolicy refreshPolicy) {
265266

266267
Assert.notNull(id, "Cannot delete entity with id 'null'.");
267268

268-
doDelete(id, getIndexCoordinates(), refreshPolicy);
269+
doDelete(id, null, getIndexCoordinates(), refreshPolicy);
269270
}
270271

271272
@Override
272273
public void delete(T entity) {
273274

274275
Assert.notNull(entity, "Cannot delete 'null' entity.");
275276

276-
doDelete(extractIdFromBean(entity), getIndexCoordinates());
277+
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates());
277278
}
278279

279280
@Override
280281
public void delete(T entity, @Nullable RefreshPolicy refreshPolicy) {
281282

282283
Assert.notNull(entity, "Cannot delete 'null' entity.");
283284

284-
doDelete(extractIdFromBean(entity), getIndexCoordinates(), refreshPolicy);
285+
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates(), refreshPolicy);
285286
}
286287

287288
@Override
@@ -352,17 +353,26 @@ private List<ID> getEntityIds(Iterable<? extends T> entities) {
352353
return ids;
353354
}
354355

355-
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates) {
356+
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) {
356357

357358
if (id != null) {
358-
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates));
359+
executeAndRefresh(operations -> {
360+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
361+
// noinspection DataFlowIssue
362+
return ops.delete(stringIdRepresentation(id), indexCoordinates);
363+
});
359364
}
360365
}
361366

362-
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates, @Nullable RefreshPolicy refreshPolicy) {
367+
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates,
368+
@Nullable RefreshPolicy refreshPolicy) {
363369

364370
if (id != null) {
365-
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates), refreshPolicy);
371+
executeAndRefresh(operations -> {
372+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
373+
// noinspection DataFlowIssue
374+
return ops.delete(stringIdRepresentation(id), indexCoordinates);
375+
}, refreshPolicy);
366376
}
367377
}
368378

src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleReactiveElasticsearchRepository.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3434
import org.springframework.data.elasticsearch.core.query.BaseQuery;
3535
import org.springframework.data.elasticsearch.core.query.Query;
36+
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
3637
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
3738
import org.springframework.lang.Nullable;
3839
import org.springframework.util.Assert;
@@ -256,7 +257,9 @@ public Mono<Void> delete(T entity) {
256257

257258
Assert.notNull(entity, "Entity must not be null!");
258259

259-
return operations.delete(entity, entityInformation.getIndexCoordinates()) //
260+
var routing = operations.getEntityRouting(entity);
261+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
262+
return ops.delete(entity, entityInformation.getIndexCoordinates()) //
260263
.then(doRefresh());
261264
}
262265

@@ -265,7 +268,9 @@ public Mono<Void> delete(T entity, @Nullable RefreshPolicy refreshPolicy) {
265268

266269
Assert.notNull(entity, "Entity must not be null!");
267270

268-
return operations.withRefreshPolicy(refreshPolicy).delete(entity, entityInformation.getIndexCoordinates()) //
271+
var routing = operations.getEntityRouting(entity);
272+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
273+
return ops.withRefreshPolicy(refreshPolicy).delete(entity, entityInformation.getIndexCoordinates()) //
269274
.then(doRefresh());
270275
}
271276

0 commit comments

Comments
 (0)