@@ -3,6 +3,7 @@ package algoliasearch
3
3
import algoliasearch .api .SearchClient
4
4
import algoliasearch .config .RequestOptions
5
5
import algoliasearch .exception .AlgoliaApiException
6
+ import algoliasearch .extension .internal .Iterable .createIterable
6
7
import algoliasearch .extension .internal .RetryUntil .{DEFAULT_DELAY , retryUntil }
7
8
import algoliasearch .search ._
8
9
@@ -354,9 +355,6 @@ package object extension {
354
355
batchSize : Int = 1000 ,
355
356
requestOptions : Option [RequestOptions ] = None
356
357
)(implicit ec : ExecutionContext ): Future [ReplaceAllObjectsResponse ] = {
357
- val requests = objects.map { record =>
358
- BatchRequest (action = Action .AddObject , body = record)
359
- }
360
358
val tmpIndexName = s " ${indexName}_tmp_ ${scala.util.Random .nextInt(100 )}"
361
359
362
360
for {
@@ -405,6 +403,11 @@ package object extension {
405
403
)
406
404
}
407
405
406
+ /**
407
+ * Check if an index exists.
408
+ * @param indexName The index name to check.
409
+ * @return A future containing a boolean indicating if the index exists.
410
+ */
408
411
def indexExists (indexName : String )(implicit ec : ExecutionContext ): Future [Boolean ] = {
409
412
try {
410
413
client.getSettings(indexName)
@@ -415,5 +418,100 @@ package object extension {
415
418
416
419
Future .successful(true )
417
420
}
421
+
422
+ /**
423
+ * Browse objects in an index.
424
+ * @param indexName The index name to browse.
425
+ * @param browseParams The browse parameters.
426
+ * @param validate The validation function. Default is to check if the cursor is defined.
427
+ * @param aggregator The aggregation function. This is where you can aggregate the results.
428
+ * @param requestOptions Additional request configuration.
429
+ * @return A future containing the last browse response.
430
+ */
431
+ def browseObjects (
432
+ indexName : String ,
433
+ browseParams : BrowseParamsObject ,
434
+ validate : BrowseResponse => Boolean = response => response.cursor.isDefined,
435
+ aggregator : BrowseResponse => Unit ,
436
+ requestOptions : Option [RequestOptions ] = None
437
+ )(implicit ec : ExecutionContext ): Future [BrowseResponse ] = {
438
+ createIterable(
439
+ execute = previousResponse =>
440
+ client.browse(indexName, Some (browseParams.copy(cursor = previousResponse.flatMap(_.cursor))), requestOptions),
441
+ validate = validate,
442
+ aggregator = Some (aggregator),
443
+ )
444
+ }
445
+
446
+ /**
447
+ * Browse rules in an index.
448
+ * @param indexName The index name to browse.
449
+ * @param searchRulesParams The search rules parameters.
450
+ * @param validate The validation function. Default is to check if the number of hits is less than the hits per page.
451
+ * @param aggregator The aggregation function. This is where you can aggregate the results.
452
+ * @param requestOptions Additional request configuration.
453
+ * @return A future containing the last search rules response.
454
+ */
455
+ def browseRules (
456
+ indexName : String ,
457
+ searchRulesParams : SearchRulesParams ,
458
+ validate : Option [SearchRulesResponse => Boolean ] = None ,
459
+ aggregator : SearchRulesResponse => Unit ,
460
+ requestOptions : Option [RequestOptions ] = None
461
+ )(implicit ec : ExecutionContext ): Future [SearchRulesResponse ] = {
462
+ val hitsPerPage = 1000
463
+
464
+ createIterable(
465
+ execute = previousResponse =>
466
+ client.searchRules(
467
+ indexName,
468
+ Some (searchRulesParams.copy(
469
+ page = previousResponse.map(_.page + 1 ).orElse(Some (0 )),
470
+ hitsPerPage = Some (hitsPerPage),
471
+ )),
472
+ requestOptions
473
+ ),
474
+ validate = validate.getOrElse((response : SearchRulesResponse ) => response.hits.length < hitsPerPage),
475
+ aggregator = Some (aggregator),
476
+ )
477
+ }
478
+
479
+ /**
480
+ * Browse synonyms in an index.
481
+ * @param indexName The index name to browse.
482
+ * @param searchSynonymsParams The search synonyms parameters.
483
+ * @param validate The validation function. Default is to check if the number of hits is less than the hits per page.
484
+ * @param aggregator The aggregation function. This is where you can aggregate the results.
485
+ * @param requestOptions Additional request configuration.
486
+ * @return A future containing the last search synonyms response.
487
+ */
488
+ def browseSynonyms (
489
+ indexName : String ,
490
+ searchSynonymsParams : SearchSynonymsParams ,
491
+ validate : Option [SearchSynonymsResponse => Boolean ] = None ,
492
+ aggregator : SearchSynonymsResponse => Unit ,
493
+ requestOptions : Option [RequestOptions ] = None
494
+ )(implicit ec : ExecutionContext ): Future [SearchSynonymsResponse ] = {
495
+ val hitsPerPage = 1000
496
+ var page = searchSynonymsParams.page.getOrElse(0 )
497
+
498
+ createIterable(
499
+ execute = _ =>
500
+ try {
501
+ client.searchSynonyms(
502
+ indexName,
503
+ Some (searchSynonymsParams.copy(
504
+ page = Some (page),
505
+ hitsPerPage = Some (hitsPerPage),
506
+ )),
507
+ requestOptions
508
+ )
509
+ } finally {
510
+ page += 1
511
+ },
512
+ validate = validate.getOrElse((response : SearchSynonymsResponse ) => response.hits.length < hitsPerPage),
513
+ aggregator = Some (aggregator),
514
+ )
515
+ }
418
516
}
419
517
}
0 commit comments