diff --git a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java index c245524aa..134a00760 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java @@ -362,6 +362,76 @@ public List bulkOperation(List queries, BulkOptions public abstract List doBulkOperation(List queries, BulkOptions bulkOptions, IndexCoordinates index); + @Override + public UpdateResponse update(T entity) { + + Assert.notNull(entity, "entity must not be null"); + + return update(entity, getIndexCoordinatesFor(entity.getClass())); + } + + @Override + public 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 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 updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) { + + ElasticsearchPersistentEntity persistentEntity = elasticsearchConverter.getMappingContext() + .getPersistentEntity(entity.getClass()); + + if (persistentEntity != null) { + PersistentPropertyAccessor 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 IndexCoordinates getIndexCoordinatesFor(Class clazz) { return getRequiredPersistentEntity(clazz).getIndexCoordinates(); } - @Override - public UpdateResponse update(T entity) { - return update(buildUpdateQueryByEntity(entity), getIndexCoordinatesFor(entity.getClass())); - } - - protected 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 updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) { - - ElasticsearchPersistentEntity persistentEntity = elasticsearchConverter.getMappingContext() - .getPersistentEntity(entity.getClass()); - - if (persistentEntity != null) { - PersistentPropertyAccessor 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); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java b/src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java index 960755bf2..294a5ca0e 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java @@ -303,6 +303,17 @@ default void bulkUpdate(List queries, IndexCoordinates index) { */ 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 the entity type + * @since 5.1 + */ + UpdateResponse update(T entity, IndexCoordinates index); + /** * Partial update of the document. *