mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 12:32:10 +00:00
Use routing info on delete operations.
Original Pull Request #2755 Closes #2754
This commit is contained in:
parent
3b93b6aea9
commit
9abcacb2e9
@ -758,6 +758,12 @@ abstract public class AbstractReactiveElasticsearchTemplate
|
|||||||
|
|
||||||
public abstract Mono<String> getClusterVersion();
|
public abstract Mono<String> getClusterVersion();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getEntityRouting(Object entity) {
|
||||||
|
return entityOperations.forEntity(entity, converter.getConversionService(), routingResolver)
|
||||||
|
.getRouting();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value class to capture client independent information from a response to an index request.
|
* Value class to capture client independent information from a response to an index request.
|
||||||
*/
|
*/
|
||||||
|
@ -77,6 +77,16 @@ public interface ReactiveElasticsearchOperations
|
|||||||
*/
|
*/
|
||||||
ReactiveClusterOperations cluster();
|
ReactiveClusterOperations cluster();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the routing for an entity.
|
||||||
|
*
|
||||||
|
* @param entity the entity
|
||||||
|
* @return the routing, may be null if not set.
|
||||||
|
* @since 5.2
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
String getEntityRouting(Object entity);
|
||||||
|
|
||||||
// region customizations
|
// region customizations
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this instance with the same configuration, but that uses a different {@link RoutingResolver} to
|
* Returns a copy of this instance with the same configuration, but that uses a different {@link RoutingResolver} to
|
||||||
|
@ -40,6 +40,7 @@ import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
|||||||
import org.springframework.data.elasticsearch.core.query.BaseQuery;
|
import org.springframework.data.elasticsearch.core.query.BaseQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
|
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.Query;
|
import org.springframework.data.elasticsearch.core.query.Query;
|
||||||
|
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
|
||||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||||
import org.springframework.data.util.StreamUtils;
|
import org.springframework.data.util.StreamUtils;
|
||||||
import org.springframework.data.util.Streamable;
|
import org.springframework.data.util.Streamable;
|
||||||
@ -257,7 +258,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
|
|
||||||
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
||||||
|
|
||||||
doDelete(id, getIndexCoordinates());
|
doDelete(id, null, getIndexCoordinates());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -265,7 +266,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
|
|
||||||
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
||||||
|
|
||||||
doDelete(id, getIndexCoordinates(), refreshPolicy);
|
doDelete(id, null, getIndexCoordinates(), refreshPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -273,7 +274,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
|
|
||||||
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
||||||
|
|
||||||
doDelete(extractIdFromBean(entity), getIndexCoordinates());
|
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -281,7 +282,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
|
|
||||||
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
||||||
|
|
||||||
doDelete(extractIdFromBean(entity), getIndexCoordinates(), refreshPolicy);
|
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates(), refreshPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -352,17 +353,26 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
|||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates) {
|
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) {
|
||||||
|
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates));
|
executeAndRefresh(operations -> {
|
||||||
|
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
|
||||||
|
// noinspection DataFlowIssue
|
||||||
|
return ops.delete(stringIdRepresentation(id), indexCoordinates);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates, @Nullable RefreshPolicy refreshPolicy) {
|
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates,
|
||||||
|
@Nullable RefreshPolicy refreshPolicy) {
|
||||||
|
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates), refreshPolicy);
|
executeAndRefresh(operations -> {
|
||||||
|
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
|
||||||
|
// noinspection DataFlowIssue
|
||||||
|
return ops.delete(stringIdRepresentation(id), indexCoordinates);
|
||||||
|
}, refreshPolicy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
|
|||||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||||
import org.springframework.data.elasticsearch.core.query.BaseQuery;
|
import org.springframework.data.elasticsearch.core.query.BaseQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.Query;
|
import org.springframework.data.elasticsearch.core.query.Query;
|
||||||
|
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
|
||||||
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
|
import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@ -256,7 +257,9 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
|
|||||||
|
|
||||||
Assert.notNull(entity, "Entity must not be null!");
|
Assert.notNull(entity, "Entity must not be null!");
|
||||||
|
|
||||||
return operations.delete(entity, entityInformation.getIndexCoordinates()) //
|
var routing = operations.getEntityRouting(entity);
|
||||||
|
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
|
||||||
|
return ops.delete(entity, entityInformation.getIndexCoordinates()) //
|
||||||
.then(doRefresh());
|
.then(doRefresh());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,7 +268,9 @@ public class SimpleReactiveElasticsearchRepository<T, ID> implements ReactiveEla
|
|||||||
|
|
||||||
Assert.notNull(entity, "Entity must not be null!");
|
Assert.notNull(entity, "Entity must not be null!");
|
||||||
|
|
||||||
return operations.withRefreshPolicy(refreshPolicy).delete(entity, entityInformation.getIndexCoordinates()) //
|
var routing = operations.getEntityRouting(entity);
|
||||||
|
var ops = routing != null ? operations.withRouting(RoutingResolver.just(routing)) : operations;
|
||||||
|
return ops.withRefreshPolicy(refreshPolicy).delete(entity, entityInformation.getIndexCoordinates()) //
|
||||||
.then(doRefresh());
|
.then(doRefresh());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user