@@ -7,7 +7,7 @@ export interface HybridArgs {
77 properties ?: string [ ] ;
88 targetVectors ?: string [ ] ;
99 fusionType ?: FusionType ;
10- searches ?: ( NearTextSubSearch | NearVectorSubSearch ) [ ] ;
10+ searches ?: HybridSubSearch [ ] ;
1111}
1212
1313export interface NearTextSubSearch {
@@ -25,91 +25,57 @@ export interface NearVectorSubSearch {
2525 targetVectors ?: string [ ] ;
2626}
2727
28+ export interface HybridSubSearch {
29+ nearText ?: NearTextSubSearch ;
30+ nearVector ?: NearVectorSubSearch ;
31+ }
32+
2833export enum FusionType {
2934 rankedFusion = 'rankedFusion' ,
3035 relativeScoreFusion = 'relativeScoreFusion' ,
3136}
3237
3338class GraphQLHybridSubSearch {
34- constructor ( ) {
35- if ( new . target === GraphQLHybridSubSearch ) {
36- throw new Error ( 'Cannot instantiate abstract class' ) ;
37- }
38- }
39-
40- static fromArgs ( args : NearTextSubSearch | NearVectorSubSearch ) : GraphQLHybridSubSearch {
41- if ( 'concepts' in args ) {
42- return new GraphQLHybridSubSearchNearText ( args ) ;
43- } else {
44- return new GraphQLHybridSubSearchNearVector ( args ) ;
45- }
46- }
47-
48- toString ( ) : string {
49- throw new Error ( 'Method not implemented.' ) ;
50- }
51- }
52-
53- class GraphQLHybridSubSearchNearText extends GraphQLHybridSubSearch {
54- private concepts : string [ ] ;
55- private certainty ?: number ;
56- private distance ?: number ;
57- private moveAwayFrom ?: Move ;
58- private moveTo ?: Move ;
59-
60- constructor ( args : NearTextSubSearch ) {
61- super ( ) ;
62- this . concepts = args . concepts ;
63- this . certainty = args . certainty ;
64- this . distance = args . distance ;
65- this . moveAwayFrom = args . moveAwayFrom ;
66- this . moveTo = args . moveTo ;
67- }
68-
69- toString ( ) : string {
70- let args = [ `concepts:${ JSON . stringify ( this . concepts ) } ` ] ;
71- if ( this . certainty ) {
72- args = [ ...args , `certainty:${ this . certainty } ` ] ;
73- }
74- if ( this . distance ) {
75- args = [ ...args , `distance:${ this . distance } ` ] ;
76- }
77- if ( this . moveTo ) {
78- args = [ ...args , parseMove ( 'moveTo' , this . moveTo ) ] ;
79- }
80- if ( this . moveAwayFrom ) {
81- args = [ ...args , parseMove ( 'moveAwayFrom' , this . moveAwayFrom ) ] ;
82- }
83- return `{${ args . join ( ',' ) } }` ;
84- }
85- }
39+ private nearText ?: NearTextSubSearch ;
40+ private nearVector ?: NearVectorSubSearch ;
8641
87- class GraphQLHybridSubSearchNearVector extends GraphQLHybridSubSearch {
88- private vector : number [ ] ;
89- private certainty ?: number ;
90- private distance ?: number ;
91- private targetVectors ?: string [ ] ;
92-
93- constructor ( args : NearVectorSubSearch ) {
94- super ( ) ;
95- this . vector = args . vector ;
96- this . certainty = args . certainty ;
97- this . distance = args . distance ;
98- this . targetVectors = args . targetVectors ;
42+ constructor ( args : HybridSubSearch ) {
43+ this . nearText = args . nearText ;
44+ this . nearVector = args . nearVector ;
9945 }
10046
10147 toString ( ) : string {
102- let args = [ `vector:${ JSON . stringify ( this . vector ) } ` ] ;
103- if ( this . certainty ) {
104- args = [ ...args , `certainty:${ this . certainty } ` ] ;
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 ( ',' ) } }` ] ;
10564 }
106- if ( this . distance ) {
107- args = [ ...args , `distance:${ this . distance } ` ] ;
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 ( ',' ) } }` ] ;
10877 }
109- if ( this . targetVectors && this . targetVectors . length > 0 ) {
110- args = [ ...args , `targetVectors:${ JSON . stringify ( this . targetVectors ) } ` ] ;
111- }
112- return `{${ args . join ( ',' ) } }` ;
78+ return `{${ outer . join ( ',' ) } }` ;
11379 }
11480}
11581
@@ -129,7 +95,7 @@ export default class GraphQLHybrid {
12995 this . properties = args . properties ;
13096 this . targetVectors = args . targetVectors ;
13197 this . fusionType = args . fusionType ;
132- this . searches = args . searches ?. map ( ( search ) => GraphQLHybridSubSearch . fromArgs ( search ) ) ;
98+ this . searches = args . searches ?. map ( ( search ) => new GraphQLHybridSubSearch ( search ) ) ;
13399 }
134100
135101 toString ( ) {
0 commit comments