mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 12:02:10 +00:00
Add update(entity, index) operation.
Original Pull Request #2394 Closes #2385
This commit is contained in:
parent
e1c8a2adeb
commit
c460a5f37d
@ -362,6 +362,76 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
public abstract List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
|
||||
IndexCoordinates index);
|
||||
|
||||
@Override
|
||||
public <T> UpdateResponse update(T entity) {
|
||||
|
||||
Assert.notNull(entity, "entity must not be null");
|
||||
|
||||
return update(entity, getIndexCoordinatesFor(entity.getClass()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> UpdateResponse update(T entity, IndexCoordinates index) {
|
||||
|
||||
Assert.notNull(entity, "entity must not be null");
|
||||
Assert.notNull(index, "index must not be null");
|
||||
|
||||
return update(buildUpdateQueryByEntity(entity), index);
|
||||
}
|
||||
|
||||
protected <T> UpdateQuery buildUpdateQueryByEntity(T entity) {
|
||||
|
||||
Assert.notNull(entity, "entity must not be null");
|
||||
|
||||
String id = getEntityId(entity);
|
||||
Assert.notNull(id, "entity must have an id that is notnull");
|
||||
|
||||
UpdateQuery.Builder updateQueryBuilder = UpdateQuery.builder(id)
|
||||
.withDocument(elasticsearchConverter.mapObject(entity));
|
||||
|
||||
String routing = getEntityRouting(entity);
|
||||
if (StringUtils.hasText(routing)) {
|
||||
updateQueryBuilder.withRouting(routing);
|
||||
}
|
||||
|
||||
return updateQueryBuilder.build();
|
||||
}
|
||||
|
||||
protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
|
||||
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
|
||||
.getPersistentEntity(entity.getClass());
|
||||
|
||||
if (persistentEntity != null) {
|
||||
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
|
||||
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
// Only deal with text because ES generated Ids are strings!
|
||||
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
|
||||
&& idProperty.getType().isAssignableFrom(String.class)) {
|
||||
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
|
||||
}
|
||||
|
||||
if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null
|
||||
&& persistentEntity.hasSeqNoPrimaryTermProperty()) {
|
||||
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
|
||||
// noinspection ConstantConditions
|
||||
propertyAccessor.setProperty(seqNoPrimaryTermProperty,
|
||||
new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
|
||||
}
|
||||
|
||||
if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
|
||||
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
|
||||
// noinspection ConstantConditions
|
||||
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
|
||||
}
|
||||
|
||||
// noinspection unchecked
|
||||
return (T) propertyAccessor.getBean();
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region SearchOperations
|
||||
@ -463,64 +533,6 @@ public abstract class AbstractElasticsearchTemplate implements ElasticsearchOper
|
||||
return getRequiredPersistentEntity(clazz).getIndexCoordinates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> UpdateResponse update(T entity) {
|
||||
return update(buildUpdateQueryByEntity(entity), getIndexCoordinatesFor(entity.getClass()));
|
||||
}
|
||||
|
||||
protected <T> UpdateQuery buildUpdateQueryByEntity(T entity) {
|
||||
|
||||
Assert.notNull(entity, "entity must not be null");
|
||||
|
||||
String id = getEntityId(entity);
|
||||
Assert.notNull(id, "entity must have an id that is notnull");
|
||||
|
||||
UpdateQuery.Builder updateQueryBuilder = UpdateQuery.builder(id)
|
||||
.withDocument(elasticsearchConverter.mapObject(entity));
|
||||
|
||||
String routing = getEntityRouting(entity);
|
||||
if (StringUtils.hasText(routing)) {
|
||||
updateQueryBuilder.withRouting(routing);
|
||||
}
|
||||
|
||||
return updateQueryBuilder.build();
|
||||
}
|
||||
|
||||
protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
|
||||
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
|
||||
.getPersistentEntity(entity.getClass());
|
||||
|
||||
if (persistentEntity != null) {
|
||||
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
|
||||
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
// Only deal with text because ES generated Ids are strings!
|
||||
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
|
||||
&& idProperty.getType().isAssignableFrom(String.class)) {
|
||||
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
|
||||
}
|
||||
|
||||
if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null
|
||||
&& persistentEntity.hasSeqNoPrimaryTermProperty()) {
|
||||
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
|
||||
// noinspection ConstantConditions
|
||||
propertyAccessor.setProperty(seqNoPrimaryTermProperty,
|
||||
new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
|
||||
}
|
||||
|
||||
if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
|
||||
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
|
||||
// noinspection ConstantConditions
|
||||
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
|
||||
}
|
||||
|
||||
// noinspection unchecked
|
||||
return (T) propertyAccessor.getBean();
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) {
|
||||
return elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz);
|
||||
}
|
||||
|
@ -303,6 +303,17 @@ public interface DocumentOperations {
|
||||
*/
|
||||
<T> UpdateResponse update(T entity);
|
||||
|
||||
/**
|
||||
* Partially update a document by the given entity.
|
||||
*
|
||||
* @param entity the entity to update partially, must not be {@literal null}.
|
||||
* @param index the index to use for the update instead of the one defined by the entity, must not be null
|
||||
* @return the update response
|
||||
* @param <T> the entity type
|
||||
* @since 5.1
|
||||
*/
|
||||
<T> UpdateResponse update(T entity, IndexCoordinates index);
|
||||
|
||||
/**
|
||||
* Partial update of the document.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user