Skip to content

Commit c460a5f

Browse files
authored
Add update(entity, index) operation.
Original Pull Request #2394 Closes #2385
1 parent e1c8a2a commit c460a5f

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed

src/main/java/org/springframework/data/elasticsearch/core/AbstractElasticsearchTemplate.java

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,76 @@ public List<IndexedObjectInformation> bulkOperation(List<?> queries, BulkOptions
362362
public abstract List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
363363
IndexCoordinates index);
364364

365+
@Override
366+
public <T> UpdateResponse update(T entity) {
367+
368+
Assert.notNull(entity, "entity must not be null");
369+
370+
return update(entity, getIndexCoordinatesFor(entity.getClass()));
371+
}
372+
373+
@Override
374+
public <T> UpdateResponse update(T entity, IndexCoordinates index) {
375+
376+
Assert.notNull(entity, "entity must not be null");
377+
Assert.notNull(index, "index must not be null");
378+
379+
return update(buildUpdateQueryByEntity(entity), index);
380+
}
381+
382+
protected <T> UpdateQuery buildUpdateQueryByEntity(T entity) {
383+
384+
Assert.notNull(entity, "entity must not be null");
385+
386+
String id = getEntityId(entity);
387+
Assert.notNull(id, "entity must have an id that is notnull");
388+
389+
UpdateQuery.Builder updateQueryBuilder = UpdateQuery.builder(id)
390+
.withDocument(elasticsearchConverter.mapObject(entity));
391+
392+
String routing = getEntityRouting(entity);
393+
if (StringUtils.hasText(routing)) {
394+
updateQueryBuilder.withRouting(routing);
395+
}
396+
397+
return updateQueryBuilder.build();
398+
}
399+
400+
protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
401+
402+
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
403+
.getPersistentEntity(entity.getClass());
404+
405+
if (persistentEntity != null) {
406+
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
407+
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
408+
409+
// Only deal with text because ES generated Ids are strings!
410+
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
411+
&& idProperty.getType().isAssignableFrom(String.class)) {
412+
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
413+
}
414+
415+
if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null
416+
&& persistentEntity.hasSeqNoPrimaryTermProperty()) {
417+
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
418+
// noinspection ConstantConditions
419+
propertyAccessor.setProperty(seqNoPrimaryTermProperty,
420+
new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
421+
}
422+
423+
if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
424+
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
425+
// noinspection ConstantConditions
426+
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
427+
}
428+
429+
// noinspection unchecked
430+
return (T) propertyAccessor.getBean();
431+
}
432+
return entity;
433+
}
434+
365435
// endregion
366436

367437
// region SearchOperations
@@ -463,64 +533,6 @@ public IndexCoordinates getIndexCoordinatesFor(Class<?> clazz) {
463533
return getRequiredPersistentEntity(clazz).getIndexCoordinates();
464534
}
465535

466-
@Override
467-
public <T> UpdateResponse update(T entity) {
468-
return update(buildUpdateQueryByEntity(entity), getIndexCoordinatesFor(entity.getClass()));
469-
}
470-
471-
protected <T> UpdateQuery buildUpdateQueryByEntity(T entity) {
472-
473-
Assert.notNull(entity, "entity must not be null");
474-
475-
String id = getEntityId(entity);
476-
Assert.notNull(id, "entity must have an id that is notnull");
477-
478-
UpdateQuery.Builder updateQueryBuilder = UpdateQuery.builder(id)
479-
.withDocument(elasticsearchConverter.mapObject(entity));
480-
481-
String routing = getEntityRouting(entity);
482-
if (StringUtils.hasText(routing)) {
483-
updateQueryBuilder.withRouting(routing);
484-
}
485-
486-
return updateQueryBuilder.build();
487-
}
488-
489-
protected <T> T updateIndexedObject(T entity, IndexedObjectInformation indexedObjectInformation) {
490-
491-
ElasticsearchPersistentEntity<?> persistentEntity = elasticsearchConverter.getMappingContext()
492-
.getPersistentEntity(entity.getClass());
493-
494-
if (persistentEntity != null) {
495-
PersistentPropertyAccessor<Object> propertyAccessor = persistentEntity.getPropertyAccessor(entity);
496-
ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
497-
498-
// Only deal with text because ES generated Ids are strings!
499-
if (indexedObjectInformation.getId() != null && idProperty != null && idProperty.isWritable()
500-
&& idProperty.getType().isAssignableFrom(String.class)) {
501-
propertyAccessor.setProperty(idProperty, indexedObjectInformation.getId());
502-
}
503-
504-
if (indexedObjectInformation.getSeqNo() != null && indexedObjectInformation.getPrimaryTerm() != null
505-
&& persistentEntity.hasSeqNoPrimaryTermProperty()) {
506-
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity.getSeqNoPrimaryTermProperty();
507-
// noinspection ConstantConditions
508-
propertyAccessor.setProperty(seqNoPrimaryTermProperty,
509-
new SeqNoPrimaryTerm(indexedObjectInformation.getSeqNo(), indexedObjectInformation.getPrimaryTerm()));
510-
}
511-
512-
if (indexedObjectInformation.getVersion() != null && persistentEntity.hasVersionProperty()) {
513-
ElasticsearchPersistentProperty versionProperty = persistentEntity.getVersionProperty();
514-
// noinspection ConstantConditions
515-
propertyAccessor.setProperty(versionProperty, indexedObjectInformation.getVersion());
516-
}
517-
518-
// noinspection unchecked
519-
return (T) propertyAccessor.getBean();
520-
}
521-
return entity;
522-
}
523-
524536
ElasticsearchPersistentEntity<?> getRequiredPersistentEntity(Class<?> clazz) {
525537
return elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz);
526538
}

src/main/java/org/springframework/data/elasticsearch/core/DocumentOperations.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ default void bulkUpdate(List<UpdateQuery> queries, IndexCoordinates index) {
303303
*/
304304
<T> UpdateResponse update(T entity);
305305

306+
/**
307+
* Partially update a document by the given entity.
308+
*
309+
* @param entity the entity to update partially, must not be {@literal null}.
310+
* @param index the index to use for the update instead of the one defined by the entity, must not be null
311+
* @return the update response
312+
* @param <T> the entity type
313+
* @since 5.1
314+
*/
315+
<T> UpdateResponse update(T entity, IndexCoordinates index);
316+
306317
/**
307318
* Partial update of the document.
308319
*

0 commit comments

Comments
 (0)