Skip to content

Commit 64cf956

Browse files
committed
Use routing info on delete operations.
Original Pull Request #2755 Closes #2754 (cherry picked from commit 9abcacb)
1 parent 2c85717 commit 64cf956

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

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

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

749749
public abstract Mono<String> getClusterVersion();
750750

751+
@Nullable
752+
public String getEntityRouting(Object entity) {
753+
return entityOperations.forEntity(entity, converter.getConversionService(), routingResolver)
754+
.getRouting();
755+
}
756+
751757
/**
752758
* Value class to capture client independent information from a response to an index request.
753759
*/

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

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ public interface ReactiveElasticsearchOperations
117117
ReactiveClusterOperations cluster();
118118

119119
// region routing
120+
/**
121+
* gets the routing for an entity.
122+
*
123+
* @param entity the entity
124+
* @return the routing, may be null if not set.
125+
* @since 5.2
126+
*/
127+
@Nullable
128+
String getEntityRouting(Object entity);
129+
130+
// region customizations
120131
/**
121132
* Returns a copy of this instance with the same configuration, but that uses a different {@link RoutingResolver} to
122133
* obtain routing information.

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.data.elasticsearch.core.query.BaseQuery;
4040
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
4141
import org.springframework.data.elasticsearch.core.query.Query;
42+
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
4243
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
4344
import org.springframework.data.util.StreamUtils;
4445
import org.springframework.data.util.Streamable;
@@ -223,15 +224,15 @@ public void deleteById(ID id) {
223224

224225
Assert.notNull(id, "Cannot delete entity with id 'null'.");
225226

226-
doDelete(id, getIndexCoordinates());
227+
doDelete(id, null, getIndexCoordinates());
227228
}
228229

229230
@Override
230231
public void delete(T entity) {
231232

232233
Assert.notNull(entity, "Cannot delete 'null' entity.");
233234

234-
doDelete(extractIdFromBean(entity), getIndexCoordinates());
235+
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates());
235236
}
236237

237238
@Override
@@ -271,10 +272,14 @@ public void deleteAll(Iterable<? extends T> entities) {
271272
deleteAllById(ids);
272273
}
273274

274-
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates) {
275+
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) {
275276

276277
if (id != null) {
277-
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates));
278+
executeAndRefresh(operations -> {
279+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
280+
// noinspection DataFlowIssue
281+
return ops.delete(stringIdRepresentation(id), indexCoordinates);
282+
});
278283
}
279284
}
280285

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3232
import org.springframework.data.elasticsearch.core.query.BaseQuery;
3333
import org.springframework.data.elasticsearch.core.query.Query;
34+
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
3435
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
3536
import org.springframework.util.Assert;
3637

@@ -196,7 +197,10 @@ public Mono<Void> deleteById(Publisher<ID> id) {
196197
public Mono<Void> delete(T entity) {
197198

198199
Assert.notNull(entity, "Entity must not be null!");
199-
return operations.delete(entity, entityInformation.getIndexCoordinates()) //
200+
201+
var routing = operations.getEntityRouting(entity);
202+
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
203+
return ops.delete(entity, entityInformation.getIndexCoordinates()) //
200204
.then(doRefresh());
201205
}
202206

0 commit comments

Comments
 (0)