-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -74,6 +75,7 @@ | |
* @author Subhobrata Dey | ||
* @author Steven Pearce | ||
* @author Anton Naydenov | ||
* @author Haibo Liu | ||
*/ | ||
public abstract class AbstractElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware { | ||
|
||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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"); | ||
puppylpg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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() | ||
|
@@ -508,7 +529,7 @@ ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) { | |
} | ||
|
||
@Nullable | ||
private String getEntityId(Object entity) { | ||
public String getEntityId(Object entity) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That change is not necessary for this issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
(Then combine id/routing/source to a UpdateQuery.) The latter two methods have public access while the first one is If this PR is merged, seems that there is no need to access Anyway, I'll not modify this line to |
||
Object id = entityOperations.forEntity(entity, elasticsearchConverter.getConversionService(), routingResolver) | ||
.getId(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.