Skip to content

Commit 4f13fd7

Browse files
committed
Add initial implementation
1 parent c83e6e7 commit 4f13fd7

File tree

6 files changed

+253
-28
lines changed

6 files changed

+253
-28
lines changed

src/collections/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,4 @@ export * from './references/index.js';
306306
export * from './sort/index.js';
307307
export * from './tenants/index.js';
308308
export * from './types/index.js';
309+
export * from './vectors/index.js';

src/collections/query/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
NearMediaType,
2828
NearOptions,
2929
NearTextOptions,
30+
NearVectorInputType,
3031
Query,
3132
QueryReturn,
3233
SearchOptions,
@@ -255,9 +256,9 @@ class QueryManager<T> implements Query<T> {
255256
.then((reply) => this.parseGroupByReply(opts, reply));
256257
}
257258

258-
public nearVector(vector: number[], opts?: BaseNearOptions<T>): Promise<WeaviateReturn<T>>;
259-
public nearVector(vector: number[], opts: GroupByNearOptions<T>): Promise<GroupByReturn<T>>;
260-
public nearVector(vector: number[], opts?: NearOptions<T>): QueryReturn<T> {
259+
public nearVector(vector: NearVectorInputType, opts?: BaseNearOptions<T>): Promise<WeaviateReturn<T>>;
260+
public nearVector(vector: NearVectorInputType, opts: GroupByNearOptions<T>): Promise<GroupByReturn<T>>;
261+
public nearVector(vector: NearVectorInputType, opts?: NearOptions<T>): QueryReturn<T> {
261262
return this.checkSupportForNamedVectors(opts)
262263
.then(() => this.connection.search(this.name, this.consistencyLevel, this.tenant))
263264
.then((search) =>

src/collections/query/types.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FilterValue } from '../filters/index.js';
2+
import { MultiTargetVectorJoin } from '../index.js';
23
import { Sorting } from '../sort/classes.js';
34
import {
45
GroupByOptions,
@@ -165,6 +166,10 @@ export type GroupByNearTextOptions<T> = BaseNearTextOptions<T> & {
165166
/** The type of the media to search for in the `query.nearMedia` method */
166167
export type NearMediaType = 'audio' | 'depth' | 'image' | 'imu' | 'thermal' | 'video';
167168

169+
export type NearVectorInputType = number[] | number[][] | Record<string, number[]>;
170+
171+
export type TargetVectorInputType = string | string[] | MultiTargetVectorJoin;
172+
168173
interface Bm25<T> {
169174
/**
170175
* Search for objects in this collection using the keyword-based BM25 algorithm.
@@ -433,35 +438,35 @@ interface NearVector<T> {
433438
*
434439
* This overload is for performing a search without the `groupBy` param.
435440
*
436-
* @param {number[]} vector - The vector to search for.
441+
* @param {NearVectorInputType} vector - The vector(s) to search on.
437442
* @param {BaseNearOptions<T>} [opts] - The available options for the search excluding the `groupBy` param.
438443
* @returns {Promise<WeaviateReturn<T>>} - The result of the search within the fetched collection.
439444
*/
440-
nearVector(vector: number[], opts?: BaseNearOptions<T>): Promise<WeaviateReturn<T>>;
445+
nearVector(vector: NearVectorInputType, opts?: BaseNearOptions<T>): Promise<WeaviateReturn<T>>;
441446
/**
442447
* Search for objects by vector in this collection using a vector-based similarity search.
443448
*
444449
* See the [docs](https://weaviate.io/developers/weaviate/search/similarity) for a more detailed explanation.
445450
*
446451
* This overload is for performing a search with the `groupBy` param.
447452
*
448-
* @param {number[]} vector - The vector to search for.
453+
* @param {NearVectorInputType} vector - The vector(s) to search for.
449454
* @param {GroupByNearOptions<T>} opts - The available options for the search including the `groupBy` param.
450455
* @returns {Promise<GroupByReturn<T>>} - The group by result of the search within the fetched collection.
451456
*/
452-
nearVector(vector: number[], opts: GroupByNearOptions<T>): Promise<GroupByReturn<T>>;
457+
nearVector(vector: NearVectorInputType, opts: GroupByNearOptions<T>): Promise<GroupByReturn<T>>;
453458
/**
454459
* Search for objects by vector in this collection using a vector-based similarity search.
455460
*
456461
* See the [docs](https://weaviate.io/developers/weaviate/search/similarity) for a more detailed explanation.
457462
*
458463
* This overload is for performing a search with a programmatically defined `opts` param.
459464
*
460-
* @param {number[]} vector - The vector to search for.
465+
* @param {NearVectorInputType} vector - The vector(s) to search for.
461466
* @param {NearOptions<T>} [opts] - The available options for the search.
462467
* @returns {QueryReturn<T>} - The result of the search within the fetched collection.
463468
*/
464-
nearVector(vector: number[], opts?: NearOptions<T>): QueryReturn<T>;
469+
nearVector(vector: NearVectorInputType, opts?: NearOptions<T>): QueryReturn<T>;
465470
}
466471

467472
/** All the available methods on the `.query` namespace. */

src/collections/query/utils.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { MultiTargetVectorJoin } from '../index.js';
2+
import { NearVectorInputType, TargetVectorInputType } from './types.js';
3+
4+
export class NearVectorInputGuards {
5+
public static is1DArray(input: NearVectorInputType): input is number[] {
6+
return Array.isArray(input) && input.length > 0 && !Array.isArray(input[0]);
7+
}
8+
9+
public static is2DArray(input: NearVectorInputType): input is number[][] {
10+
return Array.isArray(input) && input.length > 0 && Array.isArray(input[0]);
11+
}
12+
13+
public static isObject(input: NearVectorInputType): input is Record<string, number[]> {
14+
return !Array.isArray(input);
15+
}
16+
}
17+
18+
export class TargetVectorInputGuards {
19+
public static isSingle(input: TargetVectorInputType): input is string {
20+
return typeof input === 'string';
21+
}
22+
23+
public static isMulti(input: TargetVectorInputType): input is string[] {
24+
return Array.isArray(input);
25+
}
26+
27+
public static isMultiJoin(input: TargetVectorInputType): input is MultiTargetVectorJoin {
28+
return (input as MultiTargetVectorJoin).combination !== undefined;
29+
}
30+
}

0 commit comments

Comments
 (0)