Skip to content

Add update(entity, index) operation #2394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,76 @@ public List<IndexedObjectInformation> bulkOperation(List<?> queries, BulkOptions
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
Expand Down Expand Up @@ -463,64 +533,6 @@ public IndexCoordinates getIndexCoordinatesFor(Class<?> clazz) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,17 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
*/
<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.
*
Expand Down