DATAES-901 - Operations deleting an entity should use a routing deducted from the entity-the-entity.

Original PR: #502
This commit is contained in:
Peter-Josef Meisch 2020-08-11 22:24:23 +02:00 committed by GitHub
parent 73bf3dd988
commit 2b6e639951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 23 deletions

View File

@ -535,15 +535,20 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
}
@Nullable
private String getEntityRouting(Object entity) {
ElasticsearchPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass());
ElasticsearchPersistentProperty joinProperty = persistentEntity.getJoinFieldProperty();
public String getEntityRouting(Object entity) {
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
.getPersistentEntity(entity.getClass());
if (joinProperty != null) {
Object joinField = persistentEntity.getPropertyAccessor(entity).getProperty(joinProperty);
if (joinField != null && JoinField.class.isAssignableFrom(joinField.getClass())
&& ((JoinField<?>) joinField).getParent() != null) {
return elasticsearchConverter.convertId(((JoinField<?>) joinField).getParent());
if (persistentEntity != null) {
ElasticsearchPersistentProperty joinProperty = persistentEntity.getJoinFieldProperty();
if (joinProperty != null) {
Object joinField = persistentEntity.getPropertyAccessor(entity).getProperty(joinProperty);
if (joinField != null && JoinField.class.isAssignableFrom(joinField.getClass())
&& ((JoinField<?>) joinField).getParent() != null) {
return elasticsearchConverter.convertId(((JoinField<?>) joinField).getParent());
}
}
}

View File

@ -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.

View File

@ -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 .

View File

@ -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());
}

View File

@ -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();
}

View File

@ -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));
});
}

View File

@ -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);
}
return deleteRequest;
}
public DeleteRequestBuilder deleteRequestBuilder(Client client, String id, IndexCoordinates index) {
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

View File

@ -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));
}
}