@@ -3,10 +3,11 @@ import Connection from '../../connection/grpc.js';
3
3
import { ConsistencyLevel } from '../../data/index.js' ;
4
4
import { DbVersionSupport } from '../../utils/dbVersion.js' ;
5
5
6
- import { WeaviateInvalidInputError , WeaviateUnsupportedFeatureError } from '../../errors.js' ;
6
+ import { WeaviateInvalidInputError } from '../../errors.js' ;
7
7
import { toBase64FromMedia } from '../../index.js' ;
8
8
import { SearchReply } from '../../proto/v1/search_get.js' ;
9
9
import { Deserialize } from '../deserialize/index.js' ;
10
+ import { Check } from '../query/check.js' ;
10
11
import {
11
12
BaseBm25Options ,
12
13
BaseHybridOptions ,
@@ -34,24 +35,10 @@ import {
34
35
import { Generate } from './types.js' ;
35
36
36
37
class GenerateManager < T > implements Generate < T > {
37
- private connection : Connection ;
38
- private name : string ;
39
- private dbVersionSupport : DbVersionSupport ;
40
- private consistencyLevel ?: ConsistencyLevel ;
41
- private tenant ?: string ;
38
+ private check : Check < T > ;
42
39
43
- private constructor (
44
- connection : Connection ,
45
- name : string ,
46
- dbVersionSupport : DbVersionSupport ,
47
- consistencyLevel ?: ConsistencyLevel ,
48
- tenant ?: string
49
- ) {
50
- this . connection = connection ;
51
- this . name = name ;
52
- this . dbVersionSupport = dbVersionSupport ;
53
- this . consistencyLevel = consistencyLevel ;
54
- this . tenant = tenant ;
40
+ private constructor ( check : Check < T > ) {
41
+ this . check = check ;
55
42
}
56
43
57
44
public static use < T > (
@@ -61,78 +48,29 @@ class GenerateManager<T> implements Generate<T> {
61
48
consistencyLevel ?: ConsistencyLevel ,
62
49
tenant ?: string
63
50
) : GenerateManager < T > {
64
- return new GenerateManager < T > ( connection , name , dbVersionSupport , consistencyLevel , tenant ) ;
51
+ return new GenerateManager < T > ( new Check < T > ( connection , name , dbVersionSupport , consistencyLevel , tenant ) ) ;
65
52
}
66
53
67
- private checkSupportForNamedVectors = async ( opts ?: BaseNearOptions < T > ) => {
68
- if ( ! Serialize . isNamedVectors ( opts ) ) return ;
69
- const check = await this . dbVersionSupport . supportsNamedVectors ( ) ;
70
- if ( ! check . supports ) throw new WeaviateUnsupportedFeatureError ( check . message ) ;
71
- } ;
72
-
73
- private checkSupportForBm25AndHybridGroupByQueries = async ( query : 'Bm25' | 'Hybrid' , opts ?: any ) => {
74
- if ( ! Serialize . isGroupBy ( opts ) ) return ;
75
- const check = await this . dbVersionSupport . supportsBm25AndHybridGroupByQueries ( ) ;
76
- if ( ! check . supports ) throw new WeaviateUnsupportedFeatureError ( check . message ( query ) ) ;
77
- } ;
78
-
79
- private checkSupportForHybridNearTextAndNearVectorSubSearches = async ( opts ?: HybridOptions < T > ) => {
80
- if ( opts ?. vector === undefined || Array . isArray ( opts . vector ) ) return ;
81
- const check = await this . dbVersionSupport . supportsHybridNearTextAndNearVectorSubsearchQueries ( ) ;
82
- if ( ! check . supports ) throw new WeaviateUnsupportedFeatureError ( check . message ) ;
83
- } ;
84
-
85
- private checkSupportForMultiTargetVectorSearch = async ( opts ?: BaseNearOptions < T > ) => {
86
- if ( ! Serialize . isMultiTargetVector ( opts ) ) return false ;
87
- const check = await this . dbVersionSupport . supportsMultiTargetVectorSearch ( ) ;
88
- if ( ! check . supports ) throw new WeaviateUnsupportedFeatureError ( check . message ) ;
89
- return check . supports ;
90
- } ;
91
-
92
- private nearSearch = async ( opts ?: BaseNearOptions < T > ) => {
93
- const [ _ , supportsTargets ] = await Promise . all ( [
94
- this . checkSupportForNamedVectors ( opts ) ,
95
- this . checkSupportForMultiTargetVectorSearch ( opts ) ,
96
- ] ) ;
97
- return {
98
- search : await this . connection . search ( this . name , this . consistencyLevel , this . tenant ) ,
99
- supportsTargets,
100
- } ;
101
- } ;
102
-
103
- private hybridSearch = async ( opts ?: BaseHybridOptions < T > ) => {
104
- const [ supportsTargets ] = await Promise . all ( [
105
- this . checkSupportForMultiTargetVectorSearch ( opts ) ,
106
- this . checkSupportForNamedVectors ( opts ) ,
107
- this . checkSupportForBm25AndHybridGroupByQueries ( 'Hybrid' , opts ) ,
108
- this . checkSupportForHybridNearTextAndNearVectorSubSearches ( opts ) ,
109
- ] ) ;
110
- return {
111
- search : await this . connection . search ( this . name , this . consistencyLevel , this . tenant ) ,
112
- supportsTargets,
113
- } ;
114
- } ;
115
-
116
54
private async parseReply ( reply : SearchReply ) {
117
- const deserialize = await Deserialize . use ( this . dbVersionSupport ) ;
55
+ const deserialize = await Deserialize . use ( this . check . dbVersionSupport ) ;
118
56
return deserialize . generate < T > ( reply ) ;
119
57
}
120
58
121
59
private async parseGroupByReply (
122
60
opts : SearchOptions < T > | GroupByOptions < T > | undefined ,
123
61
reply : SearchReply
124
62
) {
125
- const deserialize = await Deserialize . use ( this . dbVersionSupport ) ;
63
+ const deserialize = await Deserialize . use ( this . check . dbVersionSupport ) ;
126
64
return Serialize . isGroupBy ( opts ) ? deserialize . generateGroupBy < T > ( reply ) : deserialize . generate < T > ( reply ) ;
127
65
}
128
66
129
67
public fetchObjects (
130
68
generate : GenerateOptions < T > ,
131
69
opts ?: FetchObjectsOptions < T >
132
70
) : Promise < GenerativeReturn < T > > {
133
- return this . checkSupportForNamedVectors ( opts )
134
- . then ( ( ) => this . connection . search ( this . name , this . consistencyLevel , this . tenant ) )
135
- . then ( ( search ) =>
71
+ return this . check
72
+ . fetchObjects ( opts )
73
+ . then ( ( { search } ) =>
136
74
search . withFetch ( {
137
75
...Serialize . fetchObjects ( opts ) ,
138
76
generative : Serialize . generative ( generate ) ,
@@ -152,12 +90,9 @@ class GenerateManager<T> implements Generate<T> {
152
90
opts : GroupByBm25Options < T >
153
91
) : Promise < GenerativeGroupByReturn < T > > ;
154
92
public bm25 ( query : string , generate : GenerateOptions < T > , opts ?: Bm25Options < T > ) : GenerateReturn < T > {
155
- return Promise . all ( [
156
- this . checkSupportForNamedVectors ( opts ) ,
157
- this . checkSupportForBm25AndHybridGroupByQueries ( 'Bm25' , opts ) ,
158
- ] )
159
- . then ( ( ) => this . connection . search ( this . name , this . consistencyLevel , this . tenant ) )
160
- . then ( ( search ) =>
93
+ return this . check
94
+ . bm25 ( opts )
95
+ . then ( ( { search } ) =>
161
96
search . withBm25 ( {
162
97
...Serialize . bm25 ( { query, ...opts } ) ,
163
98
generative : Serialize . generative ( generate ) ,
@@ -180,10 +115,17 @@ class GenerateManager<T> implements Generate<T> {
180
115
opts : GroupByHybridOptions < T >
181
116
) : Promise < GenerativeGroupByReturn < T > > ;
182
117
public hybrid ( query : string , generate : GenerateOptions < T > , opts ?: HybridOptions < T > ) : GenerateReturn < T > {
183
- return this . hybridSearch ( opts )
184
- . then ( ( { search, supportsTargets } ) =>
118
+ return this . check
119
+ . hybridSearch ( opts )
120
+ . then ( ( { search, supportsTargets, supportsVectorsForTargets, supportsWeightsForTargets } ) =>
185
121
search . withHybrid ( {
186
- ...Serialize . hybrid ( { query, supportsTargets, ...opts } ) ,
122
+ ...Serialize . hybrid ( {
123
+ query,
124
+ supportsTargets,
125
+ supportsVectorsForTargets,
126
+ supportsWeightsForTargets,
127
+ ...opts ,
128
+ } ) ,
187
129
generative : Serialize . generative ( generate ) ,
188
130
groupBy : Serialize . isGroupBy < GroupByHybridOptions < T > > ( opts )
189
131
? Serialize . groupBy ( opts . groupBy )
@@ -208,11 +150,17 @@ class GenerateManager<T> implements Generate<T> {
208
150
generate : GenerateOptions < T > ,
209
151
opts ?: NearOptions < T >
210
152
) : GenerateReturn < T > {
211
- return this . nearSearch ( opts )
212
- . then ( ( { search, supportsTargets } ) =>
153
+ return this . check
154
+ . nearSearch ( opts )
155
+ . then ( ( { search, supportsTargets, supportsWeightsForTargets } ) =>
213
156
toBase64FromMedia ( image ) . then ( ( image ) =>
214
157
search . withNearImage ( {
215
- ...Serialize . nearImage ( { image, supportsTargets, ...( opts ? opts : { } ) } ) ,
158
+ ...Serialize . nearImage ( {
159
+ image,
160
+ supportsTargets,
161
+ supportsWeightsForTargets,
162
+ ...( opts ? opts : { } ) ,
163
+ } ) ,
216
164
generative : Serialize . generative ( generate ) ,
217
165
groupBy : Serialize . isGroupBy < GroupByNearOptions < T > > ( opts )
218
166
? Serialize . groupBy ( opts . groupBy )
@@ -234,10 +182,16 @@ class GenerateManager<T> implements Generate<T> {
234
182
opts : GroupByNearOptions < T >
235
183
) : Promise < GenerativeGroupByReturn < T > > ;
236
184
public nearObject ( id : string , generate : GenerateOptions < T > , opts ?: NearOptions < T > ) : GenerateReturn < T > {
237
- return this . nearSearch ( opts )
238
- . then ( ( { search, supportsTargets } ) =>
185
+ return this . check
186
+ . nearSearch ( opts )
187
+ . then ( ( { search, supportsTargets, supportsWeightsForTargets } ) =>
239
188
search . withNearObject ( {
240
- ...Serialize . nearObject ( { id, supportsTargets, ...( opts ? opts : { } ) } ) ,
189
+ ...Serialize . nearObject ( {
190
+ id,
191
+ supportsTargets,
192
+ supportsWeightsForTargets,
193
+ ...( opts ? opts : { } ) ,
194
+ } ) ,
241
195
generative : Serialize . generative ( generate ) ,
242
196
groupBy : Serialize . isGroupBy < GroupByNearOptions < T > > ( opts )
243
197
? Serialize . groupBy ( opts . groupBy )
@@ -262,10 +216,16 @@ class GenerateManager<T> implements Generate<T> {
262
216
generate : GenerateOptions < T > ,
263
217
opts ?: NearOptions < T >
264
218
) : GenerateReturn < T > {
265
- return this . nearSearch ( opts )
266
- . then ( ( { search, supportsTargets } ) =>
219
+ return this . check
220
+ . nearSearch ( opts )
221
+ . then ( ( { search, supportsTargets, supportsWeightsForTargets } ) =>
267
222
search . withNearText ( {
268
- ...Serialize . nearText ( { query, supportsTargets, ...( opts ? opts : { } ) } ) ,
223
+ ...Serialize . nearText ( {
224
+ query,
225
+ supportsTargets,
226
+ supportsWeightsForTargets,
227
+ ...( opts ? opts : { } ) ,
228
+ } ) ,
269
229
generative : Serialize . generative ( generate ) ,
270
230
groupBy : Serialize . isGroupBy < GroupByNearOptions < T > > ( opts )
271
231
? Serialize . groupBy ( opts . groupBy )
@@ -290,10 +250,17 @@ class GenerateManager<T> implements Generate<T> {
290
250
generate : GenerateOptions < T > ,
291
251
opts ?: NearOptions < T >
292
252
) : GenerateReturn < T > {
293
- return this . nearSearch ( opts )
294
- . then ( ( { search, supportsTargets } ) =>
253
+ return this . check
254
+ . nearVector ( vector , opts )
255
+ . then ( ( { search, supportsTargets, supportsVectorsForTargets, supportsWeightsForTargets } ) =>
295
256
search . withNearVector ( {
296
- ...Serialize . nearVector ( { vector, supportsTargets, ...( opts ? opts : { } ) } ) ,
257
+ ...Serialize . nearVector ( {
258
+ vector,
259
+ supportsTargets,
260
+ supportsVectorsForTargets,
261
+ supportsWeightsForTargets,
262
+ ...( opts ? opts : { } ) ,
263
+ } ) ,
297
264
generative : Serialize . generative ( generate ) ,
298
265
groupBy : Serialize . isGroupBy < GroupByNearOptions < T > > ( opts )
299
266
? Serialize . groupBy ( opts . groupBy )
@@ -321,10 +288,15 @@ class GenerateManager<T> implements Generate<T> {
321
288
generate : GenerateOptions < T > ,
322
289
opts ?: NearOptions < T >
323
290
) : GenerateReturn < T > {
324
- return this . nearSearch ( opts )
325
- . then ( ( { search, supportsTargets } ) => {
291
+ return this . check
292
+ . nearSearch ( opts )
293
+ . then ( ( { search, supportsTargets, supportsWeightsForTargets } ) => {
326
294
let reply : Promise < SearchReply > ;
327
- const args = { supportsTargets, ...( opts ? opts : { } ) } ;
295
+ const args = {
296
+ supportsTargets,
297
+ supportsWeightsForTargets,
298
+ ...( opts ? opts : { } ) ,
299
+ } ;
328
300
const generative = Serialize . generative ( generate ) ;
329
301
const groupBy = Serialize . isGroupBy < GroupByNearOptions < T > > ( opts )
330
302
? Serialize . groupBy ( opts . groupBy )
0 commit comments