mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-01 09:42:11 +00:00
DATAES-901 - Operations deleting an entity should use a routing deducted from the entity-the-entity.
Original PR: #502
This commit is contained in:
parent
73bf3dd988
commit
2b6e639951
@ -535,8 +535,12 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getEntityRouting(Object entity) {
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
|
||||
public String getEntityRouting(Object entity) {
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
|
||||
.getPersistentEntity(entity.getClass());
|
||||
|
||||
if (persistentEntity != null) {
|
||||
|
||||
ElasticsearchPersistentProperty joinProperty = persistentEntity.getJoinFieldProperty();
|
||||
|
||||
if (joinProperty != null) {
|
||||
@ -546,6 +550,7 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
return elasticsearchConverter.convertId(((JoinField<?>) joinField).getParent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -205,6 +205,7 @@ public interface DocumentOperations {
|
||||
|
||||
/**
|
||||
* Bulk update all objects. Will do update.
|
||||
*
|
||||
* @param clazz the entity class
|
||||
* @param queries the queries to execute in bulk
|
||||
* @since 4.1
|
||||
@ -222,11 +223,24 @@ public interface DocumentOperations {
|
||||
/**
|
||||
* Delete the one object with provided id.
|
||||
*
|
||||
* @param id the document ot delete
|
||||
* @param id the document to delete
|
||||
* @param index the index from which to delete
|
||||
* @return documentId of the document deleted
|
||||
*/
|
||||
String delete(String id, IndexCoordinates index);
|
||||
default String delete(String id, IndexCoordinates index) {
|
||||
return delete(id, null, index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the one object with provided id.
|
||||
*
|
||||
* @param id the document to delete
|
||||
* @param routing the optional routing for the document to be deleted
|
||||
* @param index the index from which to delete
|
||||
* @return documentId of the document deleted
|
||||
* @since 4.1
|
||||
*/
|
||||
String delete(String id, @Nullable String routing, IndexCoordinates index);
|
||||
|
||||
/**
|
||||
* Delete the one object with provided id.
|
||||
|
@ -59,6 +59,16 @@ public interface ElasticsearchOperations extends DocumentOperations, SearchOpera
|
||||
|
||||
IndexCoordinates getIndexCoordinatesFor(Class<?> clazz);
|
||||
|
||||
/**
|
||||
* gets the routing for an entity which might be defined by a join-type relation
|
||||
*
|
||||
* @param entity the entity
|
||||
* @return the routing, may be null if not set.
|
||||
* @since 4.1
|
||||
*/
|
||||
@Nullable
|
||||
String getEntityRouting(Object entity);
|
||||
|
||||
// region IndexOperations
|
||||
/**
|
||||
* Create an index for given indexName .
|
||||
|
@ -207,12 +207,12 @@ public class ElasticsearchRestTemplate extends AbstractElasticsearchTemplate {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String delete(String id, IndexCoordinates index) {
|
||||
public String delete(String id, @Nullable String routing, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
DeleteRequest request = requestFactory.deleteRequest(elasticsearchConverter.convertId(id), index);
|
||||
DeleteRequest request = requestFactory.deleteRequest(elasticsearchConverter.convertId(id), routing, index);
|
||||
return execute(client -> client.delete(request, RequestOptions.DEFAULT).getId());
|
||||
}
|
||||
|
||||
|
@ -225,13 +225,13 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String delete(String id, IndexCoordinates index) {
|
||||
public String delete(String id, @Nullable String routing, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(id, "id must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
DeleteRequestBuilder deleteRequestBuilder = requestFactory.deleteRequestBuilder(client,
|
||||
elasticsearchConverter.convertId(id), index);
|
||||
elasticsearchConverter.convertId(id), routing, index);
|
||||
return deleteRequestBuilder.execute().actionGet().getId();
|
||||
}
|
||||
|
||||
|
@ -472,7 +472,7 @@ public class ReactiveElasticsearchTemplate implements ReactiveElasticsearchOpera
|
||||
private Mono<String> doDeleteById(String id, IndexCoordinates index) {
|
||||
|
||||
return Mono.defer(() -> {
|
||||
DeleteRequest request = requestFactory.deleteRequest(id, index);
|
||||
DeleteRequest request = requestFactory.deleteRequest(id, null, index);
|
||||
return doDelete(prepareDeleteRequest(request));
|
||||
});
|
||||
}
|
||||
|
@ -751,14 +751,29 @@ class RequestFactory {
|
||||
return deleteByQueryRequest;
|
||||
}
|
||||
|
||||
public DeleteRequest deleteRequest(String id, IndexCoordinates index) {
|
||||
public DeleteRequest deleteRequest(String id, @Nullable String routing, IndexCoordinates index) {
|
||||
String indexName = index.getIndexName();
|
||||
return new DeleteRequest(indexName, id);
|
||||
DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
|
||||
|
||||
if (routing != null) {
|
||||
deleteRequest.routing(routing);
|
||||
}
|
||||
|
||||
public DeleteRequestBuilder deleteRequestBuilder(Client client, String id, IndexCoordinates index) {
|
||||
return deleteRequest;
|
||||
}
|
||||
|
||||
public DeleteRequestBuilder deleteRequestBuilder(Client client, String id, @Nullable String routing,
|
||||
IndexCoordinates index) {
|
||||
String indexName = index.getIndexName();
|
||||
return client.prepareDelete(indexName, IndexCoordinates.TYPE, id);
|
||||
DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete();
|
||||
deleteRequestBuilder.setIndex(indexName);
|
||||
deleteRequestBuilder.setId(id);
|
||||
|
||||
if (routing != null) {
|
||||
deleteRequestBuilder.setRouting(routing);
|
||||
}
|
||||
|
||||
return deleteRequestBuilder;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
@ -271,7 +271,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
|
||||
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
||||
|
||||
doDelete(id, getIndexCoordinates());
|
||||
doDelete(id, null, getIndexCoordinates());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -279,7 +279,7 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
|
||||
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
||||
|
||||
doDelete(extractIdFromBean(entity), getIndexCoordinates());
|
||||
doDelete(extractIdFromBean(entity), operations.getEntityRouting(entity), getIndexCoordinates());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -308,10 +308,10 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
|
||||
});
|
||||
}
|
||||
|
||||
private void doDelete(@Nullable ID id, IndexCoordinates indexCoordinates) {
|
||||
private void doDelete(@Nullable ID id, @Nullable String routing, IndexCoordinates indexCoordinates) {
|
||||
|
||||
if (id != null) {
|
||||
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), indexCoordinates));
|
||||
executeAndRefresh(operations -> operations.delete(stringIdRepresentation(id), routing, indexCoordinates));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user