@@ -188,7 +188,7 @@ public <T> T save(T entity, IndexCoordinates index) {
188
188
IndexQuery query = getIndexQuery (entityAfterBeforeConvert );
189
189
doIndex (query , index );
190
190
191
- T entityAfterAfterSave = maybeCallbackAfterSave (entityAfterBeforeConvert , index );
191
+ T entityAfterAfterSave = ( T ) maybeCallbackAfterSave (query . getObject () , index );
192
192
193
193
return entityAfterAfterSave ;
194
194
}
@@ -215,13 +215,18 @@ public <T> Iterable<T> save(Iterable<T> entities, IndexCoordinates index) {
215
215
List <IndexQuery > indexQueries = Streamable .of (entities ).stream ().map (this ::getIndexQuery )
216
216
.collect (Collectors .toList ());
217
217
218
- if (!indexQueries .isEmpty ()) {
219
- List <IndexedObjectInformation > indexedObjectInformations = bulkIndex (indexQueries , index );
220
- Iterator <IndexedObjectInformation > iterator = indexedObjectInformations .iterator ();
221
- entities .forEach (entity -> updateIndexedObject (entity , iterator .next ()));
218
+ if (indexQueries .isEmpty ()) {
219
+ return Collections .emptyList ();
222
220
}
223
221
224
- return indexQueries .stream ().map (IndexQuery ::getObject ).map (entity -> (T ) entity ).collect (Collectors .toList ());
222
+ List <IndexedObjectInformation > indexedObjectInformations = bulkIndex (indexQueries , index );
223
+ Iterator <IndexedObjectInformation > iterator = indexedObjectInformations .iterator ();
224
+
225
+ // noinspection unchecked
226
+ return indexQueries .stream () //
227
+ .map (IndexQuery ::getObject ) //
228
+ .map (entity -> (T ) updateIndexedObject (entity , iterator .next ())) //
229
+ .collect (Collectors .toList ()); //
225
230
}
226
231
227
232
@ Override
@@ -419,7 +424,9 @@ public <T> SearchHits<T> search(MoreLikeThisQuery query, Class<T> clazz, IndexCo
419
424
Assert .notNull (query .getId (), "No document id defined for MoreLikeThisQuery" );
420
425
421
426
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = requestFactory .moreLikeThisQueryBuilder (query , index );
422
- return search (new NativeSearchQueryBuilder ().withQuery (moreLikeThisQueryBuilder ).withPageable (query .getPageable ()).build (), clazz , index );
427
+ return search (
428
+ new NativeSearchQueryBuilder ().withQuery (moreLikeThisQueryBuilder ).withPageable (query .getPageable ()).build (),
429
+ clazz , index );
423
430
}
424
431
425
432
@ Override
@@ -611,7 +618,7 @@ protected List<IndexedObjectInformation> checkForBulkOperationFailure(BulkRespon
611
618
}).collect (Collectors .toList ());
612
619
}
613
620
614
- protected void updateIndexedObject (Object entity , IndexedObjectInformation indexedObjectInformation ) {
621
+ protected < T > T updateIndexedObject (T entity , IndexedObjectInformation indexedObjectInformation ) {
615
622
616
623
ElasticsearchPersistentEntity <?> persistentEntity = elasticsearchConverter .getMappingContext ()
617
624
.getPersistentEntity (entity .getClass ());
@@ -621,22 +628,30 @@ protected void updateIndexedObject(Object entity, IndexedObjectInformation index
621
628
ElasticsearchPersistentProperty idProperty = persistentEntity .getIdProperty ();
622
629
623
630
// Only deal with text because ES generated Ids are strings!
624
- if (idProperty != null && idProperty .getType ().isAssignableFrom (String .class )) {
631
+ if (indexedObjectInformation .getId () != null && idProperty != null
632
+ && idProperty .getType ().isAssignableFrom (String .class )) {
625
633
propertyAccessor .setProperty (idProperty , indexedObjectInformation .getId ());
626
634
}
627
635
628
636
if (indexedObjectInformation .getSeqNo () != null && indexedObjectInformation .getPrimaryTerm () != null
629
637
&& persistentEntity .hasSeqNoPrimaryTermProperty ()) {
630
638
ElasticsearchPersistentProperty seqNoPrimaryTermProperty = persistentEntity .getSeqNoPrimaryTermProperty ();
639
+ // noinspection ConstantConditions
631
640
propertyAccessor .setProperty (seqNoPrimaryTermProperty ,
632
641
new SeqNoPrimaryTerm (indexedObjectInformation .getSeqNo (), indexedObjectInformation .getPrimaryTerm ()));
633
642
}
634
643
635
644
if (indexedObjectInformation .getVersion () != null && persistentEntity .hasVersionProperty ()) {
636
645
ElasticsearchPersistentProperty versionProperty = persistentEntity .getVersionProperty ();
646
+ // noinspection ConstantConditions
637
647
propertyAccessor .setProperty (versionProperty , indexedObjectInformation .getVersion ());
638
648
}
649
+
650
+ // noinspection unchecked
651
+ T updatedEntity = (T ) propertyAccessor .getBean ();
652
+ return updatedEntity ;
639
653
}
654
+ return entity ;
640
655
}
641
656
642
657
ElasticsearchPersistentEntity <?> getRequiredPersistentEntity (Class <?> clazz ) {
@@ -807,13 +822,16 @@ protected <T> T maybeCallbackAfterConvert(T entity, Document document, IndexCoor
807
822
808
823
protected void updateIndexedObjectsWithQueries (List <?> queries ,
809
824
List <IndexedObjectInformation > indexedObjectInformations ) {
825
+
810
826
for (int i = 0 ; i < queries .size (); i ++) {
811
827
Object query = queries .get (i );
828
+
812
829
if (query instanceof IndexQuery ) {
813
830
IndexQuery indexQuery = (IndexQuery ) query ;
814
831
Object queryObject = indexQuery .getObject ();
832
+
815
833
if (queryObject != null ) {
816
- updateIndexedObject (queryObject , indexedObjectInformations .get (i ));
834
+ indexQuery . setObject ( updateIndexedObject (queryObject , indexedObjectInformations .get (i ) ));
817
835
}
818
836
}
819
837
}
@@ -848,6 +866,10 @@ public T doWith(@Nullable Document document) {
848
866
}
849
867
850
868
T entity = reader .read (type , document );
869
+ IndexedObjectInformation indexedObjectInformation = IndexedObjectInformation .of (
870
+ document .hasId () ? document .getId () : null , document .getSeqNo (), document .getPrimaryTerm (),
871
+ document .getVersion ());
872
+ entity = updateIndexedObject (entity , indexedObjectInformation );
851
873
return maybeCallbackAfterConvert (entity , document , index );
852
874
}
853
875
}
0 commit comments