@@ -344,6 +344,29 @@ func (i *Index) browserForObjects(params searchParams, opts ...interface{}) func
344
344
// prevent issues, one may want to pass the opt.Safe(true) option. Note that
345
345
// passing this option will make the method blocking.
346
346
func (i * Index ) ReplaceAllObjects (objects interface {}, opts ... interface {}) (g * wait.Group , err error ) {
347
+ safe := iopt .ExtractSafe (opts ... ).Get ()
348
+
349
+ exists , err := i .Exists ()
350
+ if err != nil {
351
+ err = fmt .Errorf ("cannot check if the index exists: %v" , err )
352
+ return
353
+ }
354
+
355
+ if ! exists {
356
+ resSaveObjects , e := i .SaveObjects (objects , opts ... )
357
+ if e != nil {
358
+ err = fmt .Errorf ("cannot save objects to the index: %v" , e )
359
+ return
360
+ }
361
+ if safe {
362
+ if e := resSaveObjects .Wait (); e != nil {
363
+ err = fmt .Errorf ("error while waiting for saving objects to the index: %v" , e )
364
+ return
365
+ }
366
+ }
367
+ return
368
+ }
369
+
347
370
tmpIndex := i .client .InitIndex (fmt .Sprintf (
348
371
"%s_tmp_%d" ,
349
372
i .name ,
@@ -359,7 +382,6 @@ func (i *Index) ReplaceAllObjects(objects interface{}, opts ...interface{}) (g *
359
382
}()
360
383
361
384
g = wait .NewGroup ()
362
- safe := iopt .ExtractSafe (opts ... ).Get ()
363
385
optsWithScopes := opt .InsertOrReplaceOption (opts , opt .Scopes ("rules" , "settings" , "synonyms" ))
364
386
365
387
resCopyIndex , err := i .client .CopyIndex (i .name , tmpIndex .name , optsWithScopes ... )
0 commit comments