1+ import { Move , parseMove } from './nearText' ;
2+
13export interface HybridArgs {
24 alpha ?: number ;
35 query : string ;
46 vector ?: number [ ] ;
57 properties ?: string [ ] ;
68 targetVectors ?: string [ ] ;
79 fusionType ?: FusionType ;
10+ searches ?: HybridSubSearch [ ] ;
11+ }
12+
13+ export interface NearTextSubSearch {
14+ concepts : string [ ] ;
15+ certainty ?: number ;
16+ distance ?: number ;
17+ moveAwayFrom ?: Move ;
18+ moveTo ?: Move ;
19+ }
20+
21+ export interface NearVectorSubSearch {
22+ vector : number [ ] ;
23+ certainty ?: number ;
24+ distance ?: number ;
25+ targetVectors ?: string [ ] ;
26+ }
27+
28+ export interface HybridSubSearch {
29+ nearText ?: NearTextSubSearch ;
30+ nearVector ?: NearVectorSubSearch ;
831}
932
1033export enum FusionType {
1134 rankedFusion = 'rankedFusion' ,
1235 relativeScoreFusion = 'relativeScoreFusion' ,
1336}
1437
38+ class GraphQLHybridSubSearch {
39+ private nearText ?: NearTextSubSearch ;
40+ private nearVector ?: NearVectorSubSearch ;
41+
42+ constructor ( args : HybridSubSearch ) {
43+ this . nearText = args . nearText ;
44+ this . nearVector = args . nearVector ;
45+ }
46+
47+ toString ( ) : string {
48+ let outer : string [ ] = [ ] ;
49+ if ( this . nearText !== undefined ) {
50+ let inner = [ `concepts:${ JSON . stringify ( this . nearText . concepts ) } ` ] ;
51+ if ( this . nearText . certainty ) {
52+ inner = [ ...inner , `certainty:${ this . nearText . certainty } ` ] ;
53+ }
54+ if ( this . nearText . distance ) {
55+ inner = [ ...inner , `distance:${ this . nearText . distance } ` ] ;
56+ }
57+ if ( this . nearText . moveTo ) {
58+ inner = [ ...inner , parseMove ( 'moveTo' , this . nearText . moveTo ) ] ;
59+ }
60+ if ( this . nearText . moveAwayFrom ) {
61+ inner = [ ...inner , parseMove ( 'moveAwayFrom' , this . nearText . moveAwayFrom ) ] ;
62+ }
63+ outer = [ ...outer , `nearText:{${ inner . join ( ',' ) } }` ] ;
64+ }
65+ if ( this . nearVector !== undefined ) {
66+ let inner = [ `vector:${ JSON . stringify ( this . nearVector . vector ) } ` ] ;
67+ if ( this . nearVector . certainty ) {
68+ inner = [ ...inner , `certainty:${ this . nearVector . certainty } ` ] ;
69+ }
70+ if ( this . nearVector . distance ) {
71+ inner = [ ...inner , `distance:${ this . nearVector . distance } ` ] ;
72+ }
73+ if ( this . nearVector . targetVectors && this . nearVector . targetVectors . length > 0 ) {
74+ inner = [ ...inner , `targetVectors:${ JSON . stringify ( this . nearVector . targetVectors ) } ` ] ;
75+ }
76+ outer = [ ...outer , `nearVector:{${ inner . join ( ',' ) } }` ] ;
77+ }
78+ return `{${ outer . join ( ',' ) } }` ;
79+ }
80+ }
81+
1582export default class GraphQLHybrid {
1683 private alpha ?: number ;
1784 private query : string ;
1885 private vector ?: number [ ] ;
1986 private properties ?: string [ ] ;
2087 private targetVectors ?: string [ ] ;
2188 private fusionType ?: FusionType ;
89+ private searches ?: GraphQLHybridSubSearch [ ] ;
2290
2391 constructor ( args : HybridArgs ) {
2492 this . alpha = args . alpha ;
@@ -27,6 +95,7 @@ export default class GraphQLHybrid {
2795 this . properties = args . properties ;
2896 this . targetVectors = args . targetVectors ;
2997 this . fusionType = args . fusionType ;
98+ this . searches = args . searches ?. map ( ( search ) => new GraphQLHybridSubSearch ( search ) ) ;
3099 }
31100
32101 toString ( ) {
@@ -52,6 +121,10 @@ export default class GraphQLHybrid {
52121 args = [ ...args , `fusionType:${ this . fusionType } ` ] ;
53122 }
54123
124+ if ( this . searches !== undefined ) {
125+ args = [ ...args , `searches:[${ this . searches . map ( ( search ) => search . toString ( ) ) . join ( ',' ) } ]` ] ;
126+ }
127+
55128 return `{${ args . join ( ',' ) } }` ;
56129 }
57130}
0 commit comments