@@ -7,36 +7,50 @@ import { FilterValue } from '../filters/index.js';
7
7
8
8
import { WeaviateQueryError } from '../../errors.js' ;
9
9
import { Aggregator } from '../../graphql/index.js' ;
10
- import { toBase64FromMedia } from '../../index.js' ;
10
+ import { PrimitiveKeys , toBase64FromMedia } from '../../index.js' ;
11
+ import { Bm25QueryProperty } from '../query/types.js' ;
11
12
import { Serialize } from '../serialize/index.js' ;
12
13
13
- export type AggregateBaseOptions < T , M > = {
14
+ export type AggregateBaseOptions < M > = {
14
15
filters ?: FilterValue ;
15
16
returnMetrics ?: M ;
16
17
} ;
17
18
18
- export type AggregateGroupByOptions < T , M > = AggregateOptions < T , M > & {
19
- groupBy : ( keyof T & string ) | GroupByAggregate < T > ;
19
+ export type PropertyOf < T > = T extends undefined ? string : keyof T & string ;
20
+
21
+ export type AggregateGroupByOptions < T , M > = AggregateBaseOptions < M > & {
22
+ groupBy : PropertyOf < T > | GroupByAggregate < T > ;
20
23
} ;
21
24
22
25
export type GroupByAggregate < T > = {
23
- property : keyof T & string ;
26
+ property : PropertyOf < T > ;
24
27
limit ?: number ;
25
28
} ;
26
29
27
- export type AggregateOptions < T , M > = AggregateBaseOptions < T , M > ;
28
-
29
- export type AggregateBaseOverAllOptions < T , M > = AggregateBaseOptions < T , M > ;
30
+ export type AggregateOverAllOptions < M > = AggregateBaseOptions < M > ;
30
31
31
- export type AggregateNearOptions < T , M > = AggregateBaseOptions < T , M > & {
32
+ export type AggregateNearOptions < M > = AggregateBaseOptions < M > & {
32
33
certainty ?: number ;
33
34
distance ?: number ;
34
35
objectLimit ?: number ;
35
36
targetVector ?: string ;
36
37
} ;
37
38
38
- export type AggregateGroupByNearOptions < T , M > = AggregateNearOptions < T , M > & {
39
- groupBy : ( keyof T & string ) | GroupByAggregate < T > ;
39
+ export type AggregateHybridOptions < T , M > = AggregateBaseOptions < M > & {
40
+ alpha ?: number ;
41
+ maxVectorDistance ?: number ;
42
+ objectLimit ?: number ;
43
+ queryProperties ?: ( PrimitiveKeys < T > | Bm25QueryProperty < T > ) [ ] ;
44
+ targetVector ?: string ;
45
+ vector ?: number [ ] ;
46
+ } ;
47
+
48
+ export type AggregateGroupByHybridOptions < T , M > = AggregateHybridOptions < T , M > & {
49
+ groupBy : PropertyOf < T > | GroupByAggregate < T > ;
50
+ } ;
51
+
52
+ export type AggregateGroupByNearOptions < T , M > = AggregateNearOptions < M > & {
53
+ groupBy : PropertyOf < T > | GroupByAggregate < T > ;
40
54
} ;
41
55
42
56
export type AggregateBoolean = {
@@ -126,11 +140,11 @@ export type AggregateMetrics<M> = {
126
140
[ K in keyof M ] : M [ K ] extends true ? number : never ;
127
141
} ;
128
142
129
- export type MetricsProperty < T > = T extends undefined ? string : keyof T & string ;
143
+ export type MetricsProperty < T > = PropertyOf < T > ;
130
144
131
145
export const metrics = < T > ( ) => {
132
146
return {
133
- aggregate : < P extends MetricsProperty < T > > ( property : P ) => new MetricsManager < T , P > ( property ) ,
147
+ aggregate : < P extends PropertyOf < T > > ( property : P ) => new MetricsManager < T , P > ( property ) ,
134
148
} ;
135
149
} ;
136
150
@@ -143,10 +157,10 @@ export interface Metrics<T> {
143
157
144
158
See [the docs](https://weaviate.io/developers/weaviate/search/aggregate) for more details!
145
159
*/
146
- aggregate : < P extends MetricsProperty < T > > ( property : P ) => MetricsManager < T , P > ;
160
+ aggregate : < P extends PropertyOf < T > > ( property : P ) => MetricsManager < T , P > ;
147
161
}
148
162
149
- export class MetricsManager < T , P extends MetricsProperty < T > > {
163
+ export class MetricsManager < T , P extends PropertyOf < T > > {
150
164
private propertyName : P ;
151
165
152
166
constructor ( property : P ) {
@@ -346,9 +360,26 @@ class AggregateManager<T> implements Aggregate<T> {
346
360
this . tenant = tenant ;
347
361
348
362
this . groupBy = {
363
+ hybrid : < M extends PropertiesMetrics < T > | undefined = undefined > (
364
+ query : string ,
365
+ opts : AggregateGroupByHybridOptions < T , M >
366
+ ) : Promise < AggregateGroupByResult < T , M > [ ] > => {
367
+ let builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withHybrid ( {
368
+ query : query ,
369
+ alpha : opts ?. alpha ,
370
+ maxVectorDistance : opts ?. maxVectorDistance ,
371
+ properties : opts ?. queryProperties as string [ ] ,
372
+ targetVectors : opts ?. targetVector ? [ opts . targetVector ] : undefined ,
373
+ vector : opts ?. vector ,
374
+ } ) ;
375
+ if ( opts ?. objectLimit ) {
376
+ builder = builder . withObjectLimit ( opts . objectLimit ) ;
377
+ }
378
+ return this . doGroupBy ( builder ) ;
379
+ } ,
349
380
nearImage : async < M extends PropertiesMetrics < T > | undefined = undefined > (
350
381
image : string | Buffer ,
351
- opts ? : AggregateGroupByNearOptions < T , M >
382
+ opts : AggregateGroupByNearOptions < T , M >
352
383
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
353
384
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearImage ( {
354
385
image : await toBase64FromMedia ( image ) ,
@@ -363,7 +394,7 @@ class AggregateManager<T> implements Aggregate<T> {
363
394
} ,
364
395
nearObject : < M extends PropertiesMetrics < T > | undefined = undefined > (
365
396
id : string ,
366
- opts ? : AggregateGroupByNearOptions < T , M >
397
+ opts : AggregateGroupByNearOptions < T , M >
367
398
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
368
399
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearObject ( {
369
400
id : id ,
@@ -378,7 +409,7 @@ class AggregateManager<T> implements Aggregate<T> {
378
409
} ,
379
410
nearText : < M extends PropertiesMetrics < T > | undefined = undefined > (
380
411
query : string | string [ ] ,
381
- opts ? : AggregateGroupByNearOptions < T , M >
412
+ opts : AggregateGroupByNearOptions < T , M >
382
413
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
383
414
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearText ( {
384
415
concepts : Array . isArray ( query ) ? query : [ query ] ,
@@ -393,7 +424,7 @@ class AggregateManager<T> implements Aggregate<T> {
393
424
} ,
394
425
nearVector : < M extends PropertiesMetrics < T > | undefined = undefined > (
395
426
vector : number [ ] ,
396
- opts ? : AggregateGroupByNearOptions < T , M >
427
+ opts : AggregateGroupByNearOptions < T , M >
397
428
) : Promise < AggregateGroupByResult < T , M > [ ] > => {
398
429
const builder = this . base ( opts ?. returnMetrics , opts ?. filters , opts ?. groupBy ) . withNearVector ( {
399
430
vector : vector ,
@@ -419,11 +450,7 @@ class AggregateManager<T> implements Aggregate<T> {
419
450
return new Aggregator ( this . connection ) ;
420
451
}
421
452
422
- base (
423
- metrics ?: PropertiesMetrics < T > ,
424
- filters ?: FilterValue ,
425
- groupBy ?: ( keyof T & string ) | GroupByAggregate < T >
426
- ) {
453
+ base ( metrics ?: PropertiesMetrics < T > , filters ?: FilterValue , groupBy ?: PropertyOf < T > | GroupByAggregate < T > ) {
427
454
let fields = 'meta { count }' ;
428
455
let builder = this . query ( ) . withClassName ( this . name ) ;
429
456
if ( metrics ) {
@@ -489,9 +516,27 @@ class AggregateManager<T> implements Aggregate<T> {
489
516
return new AggregateManager < T > ( connection , name , dbVersionSupport , consistencyLevel , tenant ) ;
490
517
}
491
518
519
+ hybrid < M extends PropertiesMetrics < T > > (
520
+ query : string ,
521
+ opts ?: AggregateHybridOptions < T , M >
522
+ ) : Promise < AggregateResult < T , M > > {
523
+ let builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withHybrid ( {
524
+ query : query ,
525
+ alpha : opts ?. alpha ,
526
+ maxVectorDistance : opts ?. maxVectorDistance ,
527
+ properties : opts ?. queryProperties as string [ ] ,
528
+ targetVectors : opts ?. targetVector ? [ opts . targetVector ] : undefined ,
529
+ vector : opts ?. vector ,
530
+ } ) ;
531
+ if ( opts ?. objectLimit ) {
532
+ builder = builder . withObjectLimit ( opts . objectLimit ) ;
533
+ }
534
+ return this . do ( builder ) ;
535
+ }
536
+
492
537
async nearImage < M extends PropertiesMetrics < T > > (
493
538
image : string | Buffer ,
494
- opts ?: AggregateNearOptions < T , M >
539
+ opts ?: AggregateNearOptions < M >
495
540
) : Promise < AggregateResult < T , M > > {
496
541
const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearImage ( {
497
542
image : await toBase64FromMedia ( image ) ,
@@ -507,7 +552,7 @@ class AggregateManager<T> implements Aggregate<T> {
507
552
508
553
nearObject < M extends PropertiesMetrics < T > > (
509
554
id : string ,
510
- opts ?: AggregateNearOptions < T , M >
555
+ opts ?: AggregateNearOptions < M >
511
556
) : Promise < AggregateResult < T , M > > {
512
557
const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearObject ( {
513
558
id : id ,
@@ -523,7 +568,7 @@ class AggregateManager<T> implements Aggregate<T> {
523
568
524
569
nearText < M extends PropertiesMetrics < T > > (
525
570
query : string | string [ ] ,
526
- opts ?: AggregateNearOptions < T , M >
571
+ opts ?: AggregateNearOptions < M >
527
572
) : Promise < AggregateResult < T , M > > {
528
573
const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearText ( {
529
574
concepts : Array . isArray ( query ) ? query : [ query ] ,
@@ -539,7 +584,7 @@ class AggregateManager<T> implements Aggregate<T> {
539
584
540
585
nearVector < M extends PropertiesMetrics < T > > (
541
586
vector : number [ ] ,
542
- opts ?: AggregateNearOptions < T , M >
587
+ opts ?: AggregateNearOptions < M >
543
588
) : Promise < AggregateResult < T , M > > {
544
589
const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) . withNearVector ( {
545
590
vector : vector ,
@@ -553,7 +598,7 @@ class AggregateManager<T> implements Aggregate<T> {
553
598
return this . do ( builder ) ;
554
599
}
555
600
556
- overAll < M extends PropertiesMetrics < T > > ( opts ?: AggregateOptions < T , M > ) : Promise < AggregateResult < T , M > > {
601
+ overAll < M extends PropertiesMetrics < T > > ( opts ?: AggregateOverAllOptions < M > ) : Promise < AggregateResult < T , M > > {
557
602
const builder = this . base ( opts ?. returnMetrics , opts ?. filters ) ;
558
603
return this . do ( builder ) ;
559
604
}
@@ -588,7 +633,7 @@ class AggregateManager<T> implements Aggregate<T> {
588
633
prop : groupedBy . path [ 0 ] ,
589
634
value : groupedBy . value ,
590
635
} ,
591
- properties : rest . length > 0 ? rest : undefined ,
636
+ properties : rest ,
592
637
totalCount : meta ?. count ,
593
638
} ;
594
639
} )
@@ -602,6 +647,19 @@ class AggregateManager<T> implements Aggregate<T> {
602
647
export interface Aggregate < T > {
603
648
/** This namespace contains methods perform a group by search while aggregating metrics. */
604
649
groupBy : AggregateGroupBy < T > ;
650
+ /**
651
+ * Aggregate metrics over the objects returned by a hybrid search on this collection.
652
+ *
653
+ * This method requires that the objects in the collection have associated vectors.
654
+ *
655
+ * @param {string } query The text query to search for.
656
+ * @param {AggregateHybridOptions<T, M> } opts The options for the request.
657
+ * @returns {Promise<AggregateResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
658
+ */
659
+ hybrid < M extends PropertiesMetrics < T > > (
660
+ query : string ,
661
+ opts ?: AggregateHybridOptions < T , M >
662
+ ) : Promise < AggregateResult < T , M > > ;
605
663
/**
606
664
* Aggregate metrics over the objects returned by a near image vector search on this collection.
607
665
*
@@ -615,7 +673,7 @@ export interface Aggregate<T> {
615
673
*/
616
674
nearImage < M extends PropertiesMetrics < T > > (
617
675
image : string | Buffer ,
618
- opts ?: AggregateNearOptions < T , M >
676
+ opts ?: AggregateNearOptions < M >
619
677
) : Promise < AggregateResult < T , M > > ;
620
678
/**
621
679
* Aggregate metrics over the objects returned by a near object search on this collection.
@@ -630,7 +688,7 @@ export interface Aggregate<T> {
630
688
*/
631
689
nearObject < M extends PropertiesMetrics < T > > (
632
690
id : string ,
633
- opts ?: AggregateNearOptions < T , M >
691
+ opts ?: AggregateNearOptions < M >
634
692
) : Promise < AggregateResult < T , M > > ;
635
693
/**
636
694
* Aggregate metrics over the objects returned by a near vector search on this collection.
@@ -645,7 +703,7 @@ export interface Aggregate<T> {
645
703
*/
646
704
nearText < M extends PropertiesMetrics < T > > (
647
705
query : string | string [ ] ,
648
- opts ?: AggregateNearOptions < T , M >
706
+ opts ?: AggregateNearOptions < M >
649
707
) : Promise < AggregateResult < T , M > > ;
650
708
/**
651
709
* Aggregate metrics over the objects returned by a near vector search on this collection.
@@ -660,80 +718,93 @@ export interface Aggregate<T> {
660
718
*/
661
719
nearVector < M extends PropertiesMetrics < T > > (
662
720
vector : number [ ] ,
663
- opts ?: AggregateNearOptions < T , M >
721
+ opts ?: AggregateNearOptions < M >
664
722
) : Promise < AggregateResult < T , M > > ;
665
723
/**
666
724
* Aggregate metrics over all the objects in this collection without any vector search.
667
725
*
668
726
* @param {AggregateOptions<T, M> } [opts] The options for the request.
669
727
* @returns {Promise<AggregateResult<T, M>[]> } The aggregated metrics for the objects in the collection.
670
728
*/
671
- overAll < M extends PropertiesMetrics < T > > ( opts ?: AggregateOptions < T , M > ) : Promise < AggregateResult < T , M > > ;
729
+ overAll < M extends PropertiesMetrics < T > > ( opts ?: AggregateOverAllOptions < M > ) : Promise < AggregateResult < T , M > > ;
672
730
}
673
731
674
732
export interface AggregateGroupBy < T > {
675
733
/**
676
- * Aggregate metrics over the objects returned by a near image vector search on this collection.
734
+ * Aggregate metrics over the objects grouped by a specified property and returned by a hybrid search on this collection.
735
+ *
736
+ * This method requires that the objects in the collection have associated vectors.
737
+ *
738
+ * @param {string } query The text query to search for.
739
+ * @param {AggregateGroupByHybridOptions<T, M> } opts The options for the request.
740
+ * @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
741
+ */
742
+ hybrid < M extends PropertiesMetrics < T > > (
743
+ query : string ,
744
+ opts : AggregateGroupByHybridOptions < T , M >
745
+ ) : Promise < AggregateGroupByResult < T , M > [ ] > ;
746
+ /**
747
+ * Aggregate metrics over the objects grouped by a specified property and returned by a near image vector search on this collection.
677
748
*
678
749
* At least one of `certainty`, `distance`, or `object_limit` must be specified here for the vector search.
679
750
*
680
751
* This method requires a vectorizer capable of handling base64-encoded images, e.g. `img2vec-neural`, `multi2vec-clip`, and `multi2vec-bind`.
681
752
*
682
753
* @param {string | Buffer } image The image to search on. This can be a base64 string, a file path string, or a buffer.
683
- * @param {AggregateGroupByNearOptions<T, M> } [ opts] The options for the request.
754
+ * @param {AggregateGroupByNearOptions<T, M> } opts The options for the request.
684
755
* @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
685
756
*/
686
757
nearImage < M extends PropertiesMetrics < T > > (
687
758
image : string | Buffer ,
688
- opts ? : AggregateGroupByNearOptions < T , M >
759
+ opts : AggregateGroupByNearOptions < T , M >
689
760
) : Promise < AggregateGroupByResult < T , M > [ ] > ;
690
761
/**
691
- * Aggregate metrics over the objects returned by a near object search on this collection.
762
+ * Aggregate metrics over the objects grouped by a specified property and returned by a near object search on this collection.
692
763
*
693
764
* At least one of `certainty`, `distance`, or `object_limit` must be specified here for the vector search.
694
765
*
695
766
* This method requires that the objects in the collection have associated vectors.
696
767
*
697
768
* @param {string } id The ID of the object to search for.
698
- * @param {AggregateGroupByNearOptions<T, M> } [ opts] The options for the request.
769
+ * @param {AggregateGroupByNearOptions<T, M> } opts The options for the request.
699
770
* @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
700
771
*/
701
772
nearObject < M extends PropertiesMetrics < T > > (
702
773
id : string ,
703
- opts ? : AggregateGroupByNearOptions < T , M >
774
+ opts : AggregateGroupByNearOptions < T , M >
704
775
) : Promise < AggregateGroupByResult < T , M > [ ] > ;
705
776
/**
706
- * Aggregate metrics over the objects returned by a near text vector search on this collection.
777
+ * Aggregate metrics over the objects grouped by a specified property and returned by a near text vector search on this collection.
707
778
*
708
779
* At least one of `certainty`, `distance`, or `object_limit` must be specified here for the vector search.
709
780
*
710
781
* This method requires a vectorizer capable of handling text, e.g. `text2vec-contextionary`, `text2vec-openai`, etc.
711
782
*
712
783
* @param {string | string[] } query The text to search for.
713
- * @param {AggregateGroupByNearOptions<T, M> } [ opts] The options for the request.
784
+ * @param {AggregateGroupByNearOptions<T, M> } opts The options for the request.
714
785
* @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
715
786
*/
716
787
nearText < M extends PropertiesMetrics < T > > (
717
788
query : string | string [ ] ,
718
789
opts : AggregateGroupByNearOptions < T , M >
719
790
) : Promise < AggregateGroupByResult < T , M > [ ] > ;
720
791
/**
721
- * Aggregate metrics over the objects returned by a near vector search on this collection.
792
+ * Aggregate metrics over the objects grouped by a specified property and returned by a near vector search on this collection.
722
793
*
723
794
* At least one of `certainty`, `distance`, or `object_limit` must be specified here for the vector search.
724
795
*
725
796
* This method requires that the objects in the collection have associated vectors.
726
797
*
727
798
* @param {number[] } vector The vector to search for.
728
- * @param {AggregateGroupByNearOptions<T, M> } [ opts] The options for the request.
799
+ * @param {AggregateGroupByNearOptions<T, M> } opts The options for the request.
729
800
* @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects returned by the vector search.
730
801
*/
731
802
nearVector < M extends PropertiesMetrics < T > > (
732
803
vector : number [ ] ,
733
- opts ? : AggregateGroupByNearOptions < T , M >
804
+ opts : AggregateGroupByNearOptions < T , M >
734
805
) : Promise < AggregateGroupByResult < T , M > [ ] > ;
735
806
/**
736
- * Aggregate metrics over all the objects in this collection without any vector search.
807
+ * Aggregate metrics over all the objects in this collection grouped by a specified property without any vector search.
737
808
*
738
809
* @param {AggregateGroupByOptions<T, M> } [opts] The options for the request.
739
810
* @returns {Promise<AggregateGroupByResult<T, M>[]> } The aggregated metrics for the objects in the collection.
0 commit comments