Skip to content

Support partially update document by entity. #2305

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 2 commits into from
Sep 24, 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ In order to run the tests locally with `./mvnw test` you need to have docker run

== Class names of the test classes

Tset classes that do depend on the client have either `ERHLC` (when using the deprecated Elasticsearch `RestHighLevelClient`) or `ELC` (the new `ElasticsearchClient`) in their name.
Test classes that do depend on the client have either `ERHLC` (when using the deprecated Elasticsearch `RestHighLevelClient`) or `ELC` (the new `ElasticsearchClient`) in their name.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.data.elasticsearch.core.routing.DefaultRoutingResolver;
import org.springframework.data.elasticsearch.core.routing.RoutingResolver;
import org.springframework.data.elasticsearch.support.VersionInfo;
Expand All @@ -74,6 +75,7 @@
* @author Subhobrata Dey
* @author Steven Pearce
* @author Anton Naydenov
* @author Haibo Liu
*/
public abstract class AbstractElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {

Expand Down Expand Up @@ -305,7 +307,7 @@ public String delete(Object entity) {
@Override
public String delete(Object entity, IndexCoordinates index) {
String entityId = getEntityId(entity);
Assert.notNull(entityId, "entity must have an if that is notnull");
Assert.notNull(entityId, "entity must have an id that is notnull");
return this.delete(entityId, index);
}

Expand Down Expand Up @@ -468,6 +470,25 @@ public IndexCoordinates getIndexCoordinatesFor(Class<?> clazz) {
return getRequiredPersistentEntity(clazz).getIndexCoordinates();
}

@Override
public <T> UpdateResponse update(T entity) {
return update(this.buildUpdateQueryByEntity(entity), this.getIndexCoordinatesFor(entity.getClass()));
}

protected <T> UpdateQuery buildUpdateQueryByEntity(T entity) {
String id = this.getEntityId(entity);
Assert.notNull(entity, "entity must have an id that is notnull");

UpdateQuery.Builder updateQueryBuilder = UpdateQuery.builder(id)
.withDocument(elasticsearchConverter.mapObject(entity));

String routing = this.getEntityRouting(entity);
if (Objects.nonNull(routing)) {
updateQueryBuilder.withRouting(routing);
}
return updateQueryBuilder.build();
}

protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {

ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
Expand Down Expand Up @@ -508,7 +529,7 @@ ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) {
}

@Nullable
private String getEntityId(Object entity) {
public String getEntityId(Object entity) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change is not necessary for this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true this modifycation is unrelated with this PR. However,

before this PR, I update an entity using spring-data-elasticsearch in this way:

  • to get _id: ElasticsearchRestTemplate#getEntityId
  • to get _routing: ElasticsearchRestTemplate#getEntityRouting
  • to get _source: elasticsearchConverter.mapObject(entity)

(Then combine id/routing/source to a UpdateQuery.)

The latter two methods have public access while the first one is private thus must be copied from ElasticsearchRestTemplate to my class manually to use. If it was public, this copy won't happen.

If this PR is merged, seems that there is no need to access getEntityId in outer class again. However, getRouting(entity) is similar to this method in the same class and be marked as public, which is convenient to use. I think it may be better to be accessible for this method too.

Anyway, I'll not modify this line to public in new PR, but leave it for your consideration.

Object id = entityOperations.forEntity(entity, elasticsearchConverter.getConversionService(), routingResolver)
.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Peter-Josef Meisch
* @author Farid Faoudi
* @author Sijia Liu
* @author Haibo Liu
* @since 4.0
*/
public interface DocumentOperations {
Expand Down Expand Up @@ -307,6 +308,15 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
*/
ByQueryResponse delete(Query query, Class<?> clazz, IndexCoordinates index);

/**
* Partially update a document by the given entity.
*
* @param entity the entity to update partially
* @return the update response
* @param <T> the entity type
*/
<T> UpdateResponse update(T entity);

/**
* Partial update of the document.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
* @author Farid Faoudi
* @author Peer Mueller
* @author Sijia Liu
* @author Haibo Liu
*/
@SpringIntegrationTest
public abstract class ElasticsearchIntegrationTests implements NewElasticsearchClientDevelopment {
Expand Down Expand Up @@ -177,6 +178,20 @@ protected abstract Query getMatchAllQueryWithIncludesAndInlineExpressionScript(@

protected abstract Query getQueryWithRescorer();

@Test
public void shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartialUpdateByEntity() {

// given
String documentId = nextIdAsString();
String messageBeforeUpdate = "some test message";

SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(messageBeforeUpdate)
.version(System.currentTimeMillis()).build();

assertThatThrownBy(() -> operations.update(sampleEntity))
.isInstanceOf(DataAccessException.class);
}

@Test
public void shouldThrowDataAccessExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate() {

Expand Down Expand Up @@ -1499,6 +1514,33 @@ public void shouldDeleteIndexForGivenEntity() {
assertThat(indexOperations.exists()).isFalse();
}

@Test
public void shouldDoPartialUpdateBySuppliedEntityForExistingDocument() {

// given
String documentId = nextIdAsString();
String messageBeforeUpdate = "some test message";
String messageAfterUpdate = "test message";

SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message(messageBeforeUpdate)
.version(System.currentTimeMillis()).build();

IndexQuery indexQuery = getIndexQuery(sampleEntity);

operations.index(indexQuery, IndexCoordinates.of(indexNameProvider.indexName()));

// modify the entity
sampleEntity.setMessage(messageAfterUpdate);

// when
operations.update(sampleEntity);

// then
SampleEntity indexedEntity = operations.get(documentId, SampleEntity.class,
IndexCoordinates.of(indexNameProvider.indexName()));
assertThat(indexedEntity.getMessage()).isEqualTo(messageAfterUpdate);
}

@Test
public void shouldDoPartialUpdateForExistingDocument() {

Expand Down