@@ -330,13 +330,19 @@ export type AggregateResult<T, M extends PropertiesMetrics<T> | undefined = unde
330
330
totalCount : number ;
331
331
} ;
332
332
333
+ export type AggregatedGeoCoordinate = {
334
+ latitude : number ;
335
+ longitude : number ;
336
+ distance : number ;
337
+ } ;
338
+
333
339
export type AggregateGroupByResult <
334
340
T ,
335
341
M extends PropertiesMetrics < T > | undefined = undefined
336
342
> = AggregateResult < T , M > & {
337
343
groupedBy : {
338
344
prop : string ;
339
- value : string ;
345
+ value : string | number | boolean | AggregatedGeoCoordinate | string [ ] | number [ ] | boolean [ ] ;
340
346
} ;
341
347
} ;
342
348
@@ -365,10 +371,22 @@ class AggregateManager<T> implements Aggregate<T> {
365
371
this . grpcChecker = this . dbVersionSupport . supportsAggregateGRPC ( ) . then ( ( res ) => res . supports ) ;
366
372
367
373
this . groupBy = {
368
- hybrid : < M extends PropertiesMetrics < T > | undefined = undefined > (
374
+ hybrid : async < M extends PropertiesMetrics < T > > (
369
375
query : string ,
370
376
opts : AggregateGroupByHybridOptions < T , M >
371
377
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
378
+ if ( await this . grpcChecker ) {
379
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
380
+ return this . grpc ( )
381
+ . then ( ( aggregate ) =>
382
+ aggregate . withHybrid ( {
383
+ ...Serialize . aggregate . hybrid ( query , opts ) ,
384
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
385
+ limit : group . limit ,
386
+ } )
387
+ )
388
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
389
+ }
372
390
let builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withHybrid ( {
373
391
query : query ,
374
392
alpha : opts ?. alpha ,
@@ -382,12 +400,25 @@ class AggregateManager<T> implements Aggregate<T> {
382
400
}
383
401
return this . doGroupBy ( builder ) ;
384
402
} ,
385
- nearImage : async < M extends PropertiesMetrics < T > | undefined = undefined > (
403
+ nearImage : async < M extends PropertiesMetrics < T > > (
386
404
image : string | Buffer ,
387
405
opts : AggregateGroupByNearOptions < T , M >
388
406
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
407
+ const [ b64 , usesGrpc ] = await Promise . all ( [ await toBase64FromMedia ( image ) , await this . grpcChecker ] ) ;
408
+ if ( usesGrpc ) {
409
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
410
+ return this . grpc ( )
411
+ . then ( ( aggregate ) =>
412
+ aggregate . withNearImage ( {
413
+ ...Serialize . aggregate . nearImage ( b64 , opts ) ,
414
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
415
+ limit : group . limit ,
416
+ } )
417
+ )
418
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
419
+ }
389
420
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearImage ( {
390
- image : await toBase64FromMedia ( image ) ,
421
+ image : b64 ,
391
422
certainty : opts ?. certainty ,
392
423
distance : opts ?. distance ,
393
424
targetVectors : opts ?. targetVector ? [ opts . targetVector ] : undefined ,
@@ -397,10 +428,22 @@ class AggregateManager<T> implements Aggregate<T> {
397
428
}
398
429
return this . doGroupBy ( builder ) ;
399
430
} ,
400
- nearObject : < M extends PropertiesMetrics < T > | undefined = undefined > (
431
+ nearObject : async < M extends PropertiesMetrics < T > > (
401
432
id : string ,
402
433
opts : AggregateGroupByNearOptions < T , M >
403
434
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
435
+ if ( await this . grpcChecker ) {
436
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
437
+ return this . grpc ( )
438
+ . then ( ( aggregate ) =>
439
+ aggregate . withNearObject ( {
440
+ ...Serialize . aggregate . nearObject ( id , opts ) ,
441
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
442
+ limit : group . limit ,
443
+ } )
444
+ )
445
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
446
+ }
404
447
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearObject ( {
405
448
id : id ,
406
449
certainty : opts ?. certainty ,
@@ -412,10 +455,22 @@ class AggregateManager<T> implements Aggregate<T> {
412
455
}
413
456
return this . doGroupBy ( builder ) ;
414
457
} ,
415
- nearText : < M extends PropertiesMetrics < T > | undefined = undefined > (
458
+ nearText : async < M extends PropertiesMetrics < T > > (
416
459
query : string | string [ ] ,
417
460
opts : AggregateGroupByNearOptions < T , M >
418
461
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
462
+ if ( await this . grpcChecker ) {
463
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
464
+ return this . grpc ( )
465
+ . then ( ( aggregate ) =>
466
+ aggregate . withNearText ( {
467
+ ...Serialize . aggregate . nearText ( query , opts ) ,
468
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
469
+ limit : group . limit ,
470
+ } )
471
+ )
472
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
473
+ }
419
474
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearText ( {
420
475
concepts : Array . isArray ( query ) ? query : [ query ] ,
421
476
certainty : opts ?. certainty ,
@@ -427,10 +482,22 @@ class AggregateManager<T> implements Aggregate<T> {
427
482
}
428
483
return this . doGroupBy ( builder ) ;
429
484
} ,
430
- nearVector : < M extends PropertiesMetrics < T > | undefined = undefined > (
485
+ nearVector : async < M extends PropertiesMetrics < T > > (
431
486
vector : number [ ] ,
432
487
opts : AggregateGroupByNearOptions < T , M >
433
488
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
489
+ if ( await this . grpcChecker ) {
490
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
491
+ return this . grpc ( )
492
+ . then ( ( aggregate ) =>
493
+ aggregate . withNearVector ( {
494
+ ...Serialize . aggregate . nearVector ( vector , opts ) ,
495
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
496
+ limit : group . limit ,
497
+ } )
498
+ )
499
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
500
+ }
434
501
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearVector ( {
435
502
vector : vector ,
436
503
certainty : opts ?. certainty ,
@@ -442,9 +509,22 @@ class AggregateManager<T> implements Aggregate<T> {
442
509
}
443
510
return this . doGroupBy ( builder ) ;
444
511
} ,
445
- overAll : < M extends PropertiesMetrics < T > | undefined = undefined > (
512
+ overAll : async < M extends PropertiesMetrics < T > > (
446
513
opts : AggregateGroupByOptions < T , M >
447
514
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
515
+ if ( await this . grpcChecker ) {
516
+ const group = typeof opts . groupBy === 'string' ? { property : opts . groupBy } : opts . groupBy ;
517
+ return this . grpc ( )
518
+ . then ( ( aggregate ) =>
519
+ aggregate . withFetch ( {
520
+ ...Serialize . aggregate . overAll ( opts ) ,
521
+
522
+ groupBy : Serialize . aggregate . groupBy ( group ) ,
523
+ limit : group . limit ,
524
+ } )
525
+ )
526
+ . then ( ( reply ) => Deserialize . aggregateGroupBy ( reply ) ) ;
527
+ }
448
528
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) ;
449
529
return this . doGroupBy ( builder ) ;
450
530
} ,
0 commit comments