Skip to content

Commit af5df4d

Browse files
committed
Vector index
1 parent 7975c27 commit af5df4d

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/collections.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,33 @@ export interface DocumentCollection<
16851685
indexes.InvertedIndexDescription & { isNewlyCreated: boolean }
16861686
>
16871687
>;
1688+
/**
1689+
* Creates a vector index on the collection if it does not already exist.
1690+
*
1691+
* @param options - Options for creating the vector index.
1692+
*
1693+
* @example
1694+
* ```js
1695+
* const db = new Database();
1696+
* const collection = db.collection("some-collection");
1697+
* await collection.ensureIndex({
1698+
* type: "vector",
1699+
* fields: ["embedding"],
1700+
* params: {
1701+
* metric: "cosine",
1702+
* dimension: 128,
1703+
* nLists: 100
1704+
* }
1705+
* });
1706+
* ```
1707+
*/
1708+
ensureIndex(
1709+
options: indexes.EnsureVectorIndexOptions
1710+
): Promise<
1711+
connection.ArangoApiResponse<
1712+
indexes.VectorIndexDescription & { isNewlyCreated: boolean }
1713+
>
1714+
>;
16881715
/**
16891716
* Creates an index on the collection if it does not already exist.
16901717
*

src/indexes.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export type EnsureIndexOptions =
6262
| EnsureTtlIndexOptions
6363
| EnsureMdiIndexOptions
6464
| EnsureMdiPrefixedIndexOptions
65-
| EnsureInvertedIndexOptions;
65+
| EnsureInvertedIndexOptions
66+
| EnsureVectorIndexOptions;
6667

6768
/**
6869
* Shared attributes of all index creation options.
@@ -558,6 +559,56 @@ export type InvertedIndexStoredValueOptions = {
558559
*/
559560
cache?: boolean;
560561
};
562+
563+
/**
564+
* Options for creating a vector index.
565+
*/
566+
export type EnsureVectorIndexOptions = EnsureIndexOptionsType<
567+
"vector",
568+
[string],
569+
{
570+
/**
571+
* The number of threads to use for indexing. Default is 2.
572+
*/
573+
parallelism?: number;
574+
575+
/**
576+
* Vector index parameters, following Faiss configuration.
577+
*/
578+
params: {
579+
/**
580+
* Whether to use cosine or l2 (Euclidean) distance.
581+
*/
582+
metric: "cosine" | "l2";
583+
584+
/**
585+
* Vector dimension. Must match the length of vectors in documents.
586+
*/
587+
dimension: number;
588+
589+
/**
590+
* Number of Voronoi cells (centroids) for IVF. Affects accuracy and index build time.
591+
*/
592+
nLists: number;
593+
594+
/**
595+
* How many neighboring centroids to probe by default. Higher = slower, better recall.
596+
*/
597+
defaultNProbe?: number;
598+
599+
/**
600+
* Training iterations for index build. Default is 25.
601+
*/
602+
trainingIterations?: number;
603+
604+
/**
605+
* Advanced Faiss index factory string.
606+
* If not specified, defaults to IVF<nLists>,Flat.
607+
*/
608+
factory?: string;
609+
};
610+
}
611+
>;
561612
//#endregion
562613

563614
//#region IndexDescription
@@ -572,7 +623,8 @@ export type IndexDescription =
572623
| MdiIndexDescription
573624
| MdiPrefixedIndexDescription
574625
| InvertedIndexDescription
575-
| SystemIndexDescription;
626+
| SystemIndexDescription
627+
| VectorIndexDescription;
576628

577629
/**
578630
* An object representing a system index.
@@ -862,6 +914,26 @@ export type HiddenIndexDescription = (
862914
*/
863915
progress?: number;
864916
};
917+
918+
/**
919+
* An object representing a vector index.
920+
*/
921+
export type VectorIndexDescription = IndexDescriptionType<
922+
"vector",
923+
[string],
924+
{
925+
parallelism: number;
926+
inBackground: boolean;
927+
params: {
928+
metric: "cosine" | "l2";
929+
dimension: number;
930+
nLists: number;
931+
defaultNProbe?: number;
932+
trainingIterations?: number;
933+
factory?: string;
934+
};
935+
}
936+
>;
865937
//#endregion
866938

867939
//#region Index selectors

0 commit comments

Comments
 (0)