@@ -58,7 +58,7 @@ public async Task AddAsync(IEnumerable<T> documents, bool addToCache = false, Ti
58
58
foreach ( var doc in docs )
59
59
await _validator . ValidateAndThrowAsync ( doc ) . AnyContext ( ) ;
60
60
61
- await IndexDocumentsAsync ( docs ) . AnyContext ( ) ;
61
+ await IndexDocumentsAsync ( docs , isCreateOperation : true ) . AnyContext ( ) ;
62
62
63
63
if ( addToCache )
64
64
await AddToCacheAsync ( docs , expiresIn ) . AnyContext ( ) ;
@@ -668,7 +668,7 @@ private async Task OnDocumentsChangedAsync(ChangeType changeType, IReadOnlyColle
668
668
669
669
#endregion
670
670
671
- private async Task IndexDocumentsAsync ( IReadOnlyCollection < T > documents ) {
671
+ private async Task IndexDocumentsAsync ( IReadOnlyCollection < T > documents , bool isCreateOperation = false ) {
672
672
if ( HasMultipleIndexes ) {
673
673
foreach ( var documentGroup in documents . GroupBy ( TimeSeriesType . GetDocumentIndex ) )
674
674
await TimeSeriesType . EnsureIndexAsync ( documentGroup . First ( ) ) . AnyContext ( ) ;
@@ -677,6 +677,7 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents) {
677
677
if ( documents . Count == 1 ) {
678
678
var document = documents . Single ( ) ;
679
679
var response = await _client . IndexAsync ( document , i => {
680
+ i . OpType ( isCreateOperation ? OpType . Create : OpType . Index ) ;
680
681
i . Type ( ElasticType . Name ) ;
681
682
682
683
if ( GetParentIdFunc != null )
@@ -685,10 +686,9 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents) {
685
686
if ( GetDocumentIndexFunc != null )
686
687
i . Index ( GetDocumentIndexFunc ( document ) ) ;
687
688
688
- if ( HasVersion ) {
689
+ if ( HasVersion && isCreateOperation ) {
689
690
var versionDoc = ( IVersioned ) document ;
690
- if ( versionDoc . Version > 0 )
691
- i . Version ( versionDoc . Version ) ;
691
+ i . Version ( versionDoc . Version ) ;
692
692
}
693
693
694
694
return i ;
@@ -706,7 +706,7 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents) {
706
706
versionDoc . Version = response . Version ;
707
707
}
708
708
} else {
709
- var response = await _client . IndexManyAsync ( documents , GetParentIdFunc , GetDocumentIndexFunc , ElasticType . Name ) . AnyContext ( ) ;
709
+ var response = await _client . IndexManyAsync ( documents , GetParentIdFunc , GetDocumentIndexFunc , ElasticType . Name , isCreateOperation ) . AnyContext ( ) ;
710
710
_logger . Trace ( ( ) => response . GetRequest ( ) ) ;
711
711
712
712
if ( HasVersion ) {
@@ -723,12 +723,26 @@ private async Task IndexDocumentsAsync(IReadOnlyCollection<T> documents) {
723
723
}
724
724
}
725
725
726
+ var allErrors = response . ItemsWithErrors . ToList ( ) ;
727
+ if ( allErrors . Count > 0 ) {
728
+ var retryableIds = allErrors . Where ( e => e . Status == 429 || e . Status == 503 ) . Select ( e => e . Id ) . ToList ( ) ;
729
+ if ( retryableIds . Count > 0 ) {
730
+ var docs = documents . Where ( d => retryableIds . Contains ( d . Id ) ) . ToList ( ) ;
731
+ await IndexDocumentsAsync ( docs , isCreateOperation ) . AnyContext ( ) ;
732
+
733
+ // return as all recoverable items were retried.
734
+ if ( allErrors . Count == retryableIds . Count )
735
+ return ;
736
+ }
737
+ }
738
+
726
739
if ( ! response . IsValid ) {
727
740
string message = response . GetErrorMessage ( ) ;
728
741
_logger . Error ( ) . Exception ( response . OriginalException ) . Message ( message ) . Property ( "request" , response . GetRequest ( ) ) . Write ( ) ;
729
742
throw new ApplicationException ( message , response . OriginalException ) ;
730
743
}
731
744
}
745
+ // 429 // 503
732
746
}
733
747
734
748
protected virtual async Task AddToCacheAsync ( ICollection < T > documents , TimeSpan ? expiresIn = null ) {
0 commit comments