1- import Connection from '../../connection/index .js' ;
1+ import Connection from '../../connection/grpc .js' ;
22
33import { ConsistencyLevel } from '../../data/index.js' ;
44import { DbVersionSupport } from '../../utils/dbVersion.js' ;
55
66import { FilterValue } from '../filters/index.js' ;
77
8- import { WeaviateQueryError } from '../../errors.js' ;
8+ import { WeaviateInvalidInputError , WeaviateQueryError } from '../../errors.js' ;
99import { Aggregator } from '../../graphql/index.js' ;
1010import { PrimitiveKeys , toBase64FromMedia } from '../../index.js' ;
11- import { Bm25QueryProperty } from '../query/types.js' ;
11+ import { Deserialize } from '../deserialize/index.js' ;
12+ import { Bm25QueryProperty , NearVectorInputType } from '../query/types.js' ;
13+ import { NearVectorInputGuards } from '../query/utils.js' ;
1214import { Serialize } from '../serialize/index.js' ;
1315
1416export type AggregateBaseOptions < M > = {
@@ -63,10 +65,10 @@ export type AggregateBoolean = {
6365
6466export type AggregateDate = {
6567 count ?: number ;
66- maximum ?: number ;
67- median ?: number ;
68- minimum ?: number ;
69- mode ?: number ;
68+ maximum ?: string ;
69+ median ?: string ;
70+ minimum ?: string ;
71+ mode ?: string ;
7072} ;
7173
7274export type AggregateNumber = {
@@ -87,7 +89,7 @@ export type AggregateText = {
8789 count ?: number ;
8890 topOccurrences ?: {
8991 occurs ?: number ;
90- value ?: number ;
92+ value ?: string ;
9193 } [ ] ;
9294} ;
9395
@@ -345,6 +347,7 @@ class AggregateManager<T> implements Aggregate<T> {
345347 dbVersionSupport : DbVersionSupport ;
346348 consistencyLevel ?: ConsistencyLevel ;
347349 tenant ?: string ;
350+ grpcChecker : Promise < boolean > ;
348351
349352 private constructor (
350353 connection : Connection ,
@@ -359,6 +362,8 @@ class AggregateManager<T> implements Aggregate<T> {
359362 this . consistencyLevel = consistencyLevel ;
360363 this . tenant = tenant ;
361364
365+ this . grpcChecker = this . dbVersionSupport . supportsAggregateGRPC ( ) . then ( ( res ) => res . supports ) ;
366+
362367 this . groupBy = {
363368 hybrid : < M extends PropertiesMetrics < T > | undefined = undefined > (
364369 query : string ,
@@ -446,13 +451,15 @@ class AggregateManager<T> implements Aggregate<T> {
446451 } ;
447452 }
448453
449- query ( ) {
454+ private grpc = ( ) => this . connection . aggregate ( this . name , this . consistencyLevel , this . tenant ) ;
455+
456+ private gql ( ) {
450457 return new Aggregator ( this . connection ) ;
451458 }
452459
453460 base ( metrics ?: PropertiesMetrics < T > , filters ?: FilterValue , groupBy ?: PropertyOf < T > | GroupByAggregate < T > ) {
454461 let fields = 'meta { count }' ;
455- let builder = this . query ( ) . withClassName ( this . name ) ;
462+ let builder = this . gql ( ) . withClassName ( this . name ) ;
456463 if ( metrics ) {
457464 if ( Array . isArray ( metrics ) ) {
458465 fields += metrics . map ( ( m ) => this . metrics ( m ) ) . join ( ' ' ) ;
@@ -516,10 +523,15 @@ class AggregateManager<T> implements Aggregate<T> {
516523 return new AggregateManager < T > ( connection , name , dbVersionSupport , consistencyLevel , tenant ) ;
517524 }
518525
519- hybrid < M extends PropertiesMetrics < T > > (
526+ async hybrid < M extends PropertiesMetrics < T > > (
520527 query : string ,
521528 opts ?: AggregateHybridOptions < T , M >
522529 ) : Promise < AggregateResult < T , M > > {
530+ if ( await this . grpcChecker ) {
531+ return this . grpc ( )
532+ . then ( ( aggregate ) => aggregate . withHybrid ( Serialize . aggregate . hybrid ( query , opts ) ) )
533+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
534+ }
523535 let builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withHybrid ( {
524536 query : query ,
525537 alpha : opts ?. alpha ,
@@ -538,8 +550,14 @@ class AggregateManager<T> implements Aggregate<T> {
538550 image : string | Buffer ,
539551 opts ?: AggregateNearOptions < M >
540552 ) : Promise < AggregateResult < T , M > > {
553+ const [ b64 , usesGrpc ] = await Promise . all ( [ await toBase64FromMedia ( image ) , await this . grpcChecker ] ) ;
554+ if ( usesGrpc ) {
555+ return this . grpc ( )
556+ . then ( ( aggregate ) => aggregate . withNearImage ( Serialize . aggregate . nearImage ( b64 , opts ) ) )
557+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
558+ }
541559 const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearImage ( {
542- image : await toBase64FromMedia ( image ) ,
560+ image : b64 ,
543561 certainty : opts ?. certainty ,
544562 distance : opts ?. distance ,
545563 targetVectors : opts ?. targetVector ? [ opts . targetVector ] : undefined ,
@@ -550,10 +568,15 @@ class AggregateManager<T> implements Aggregate<T> {
550568 return this . do ( builder ) ;
551569 }
552570
553- nearObject < M extends PropertiesMetrics < T > > (
571+ async nearObject < M extends PropertiesMetrics < T > > (
554572 id : string ,
555573 opts ?: AggregateNearOptions < M >
556574 ) : Promise < AggregateResult < T , M > > {
575+ if ( await this . grpcChecker ) {
576+ return this . grpc ( )
577+ . then ( ( aggregate ) => aggregate . withNearObject ( Serialize . aggregate . nearObject ( id , opts ) ) )
578+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
579+ }
557580 const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearObject ( {
558581 id : id ,
559582 certainty : opts ?. certainty ,
@@ -566,10 +589,15 @@ class AggregateManager<T> implements Aggregate<T> {
566589 return this . do ( builder ) ;
567590 }
568591
569- nearText < M extends PropertiesMetrics < T > > (
592+ async nearText < M extends PropertiesMetrics < T > > (
570593 query : string | string [ ] ,
571594 opts ?: AggregateNearOptions < M >
572595 ) : Promise < AggregateResult < T , M > > {
596+ if ( await this . grpcChecker ) {
597+ return this . grpc ( )
598+ . then ( ( aggregate ) => aggregate . withNearText ( Serialize . aggregate . nearText ( query , opts ) ) )
599+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
600+ }
573601 const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearText ( {
574602 concepts : Array . isArray ( query ) ? query : [ query ] ,
575603 certainty : opts ?. certainty ,
@@ -582,10 +610,20 @@ class AggregateManager<T> implements Aggregate<T> {
582610 return this . do ( builder ) ;
583611 }
584612
585- nearVector < M extends PropertiesMetrics < T > > (
586- vector : number [ ] ,
613+ async nearVector < M extends PropertiesMetrics < T > > (
614+ vector : NearVectorInputType ,
587615 opts ?: AggregateNearOptions < M >
588616 ) : Promise < AggregateResult < T , M > > {
617+ if ( await this . grpcChecker ) {
618+ return this . grpc ( )
619+ . then ( ( aggregate ) => aggregate . withNearVector ( Serialize . aggregate . nearVector ( vector , opts ) ) )
620+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
621+ }
622+ if ( ! NearVectorInputGuards . is1DArray ( vector ) ) {
623+ throw new WeaviateInvalidInputError (
624+ 'Vector can only be a 1D array of numbers when using `nearVector` with <1.29 Weaviate versions.'
625+ ) ;
626+ }
589627 const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearVector ( {
590628 vector : vector ,
591629 certainty : opts ?. certainty ,
@@ -598,9 +636,15 @@ class AggregateManager<T> implements Aggregate<T> {
598636 return this . do ( builder ) ;
599637 }
600638
601- overAll < M extends PropertiesMetrics < T > > ( opts ?: AggregateOverAllOptions < M > ) : Promise < AggregateResult < T , M > > {
602- const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) ;
603- return this . do ( builder ) ;
639+ async overAll < M extends PropertiesMetrics < T > > (
640+ opts ?: AggregateOverAllOptions < M >
641+ ) : Promise < AggregateResult < T , M > > {
642+ if ( await this . grpcChecker ) {
643+ return this . grpc ( )
644+ . then ( ( aggregate ) => aggregate . withFetch ( Serialize . aggregate . overAll ( opts ) ) )
645+ . then ( ( reply ) => Deserialize . aggregate ( reply ) ) ;
646+ }
647+ return this . do ( this . base ( opts ?. returnMetrics , opts ?. filters ) ) ;
604648 }
605649
606650 do = < M extends PropertiesMetrics < T > | undefined = undefined > (
0 commit comments