Skip to content

Commit 4c93220

Browse files
committed
feat: add ability to create vector search indexes
1 parent f384834 commit 4c93220

File tree

3 files changed

+253
-144
lines changed

3 files changed

+253
-144
lines changed

src/tools/mongodb/create/createIndex.ts

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,91 @@ export class CreateIndexTool extends MongoDBToolBase {
99
protected description = "Create an index for a collection";
1010
protected argsShape = {
1111
...DbOperationArgs,
12-
keys: z.object({}).catchall(z.custom<IndexDirection>()).describe("The index definition"),
1312
name: z.string().optional().describe("The name of the index"),
13+
definition: z
14+
.discriminatedUnion("type", [
15+
z.object({
16+
type: z.literal("classic"),
17+
keys: z.object({}).catchall(z.custom<IndexDirection>()).describe("The index definition"),
18+
}),
19+
z.object({
20+
type: z.literal("vectorSearch"),
21+
fields: z
22+
.array(
23+
z.object({
24+
type: z
25+
.enum(["vector", "filter"])
26+
.describe(
27+
"Field type to use to index fields for vector search. You must specify `vector` for fields that contain vector embeddings and `filter` for additional fields to filter on."
28+
),
29+
path: z
30+
.string()
31+
.describe(
32+
"Name of the field to index. For nested fields, use dot notation to specify path to embedded fields"
33+
),
34+
numDimensions: z
35+
.number()
36+
.min(1)
37+
.max(8192)
38+
.describe(
39+
"Number of vector dimensions that MongoDB Vector Search enforces at index-time and query-time"
40+
),
41+
similarity: z
42+
.enum(["cosine", "euclidean", "dotProduct"])
43+
.describe(
44+
"Vector similarity function to use to search for top K-nearest neighbors. You can set this field only for vector-type fields."
45+
),
46+
quantization: z
47+
.enum(["none", "scalar", "binary"])
48+
.optional()
49+
.default("none")
50+
.describe(
51+
"Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float or double vectors."
52+
),
53+
})
54+
)
55+
.describe(
56+
"Definitions for the vector and filter fields to index, one definition per document. The fields array must contain at least one vector-type field definition."
57+
),
58+
}),
59+
])
60+
.describe(
61+
"The index definition. Use 'classic' for standard indexes and 'vectorSearch' for vector search indexes"
62+
),
1463
};
1564

1665
public operationType: OperationType = "create";
1766

1867
protected async execute({
1968
database,
2069
collection,
21-
keys,
2270
name,
71+
definition,
2372
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2473
const provider = await this.ensureConnected();
25-
const indexes = await provider.createIndexes(database, collection, [
26-
{
27-
key: keys,
28-
name,
29-
},
30-
]);
74+
let indexes: string[] = [];
75+
switch (definition.type) {
76+
case "classic":
77+
indexes = await provider.createIndexes(database, collection, [
78+
{
79+
key: definition.keys,
80+
name,
81+
},
82+
]);
83+
84+
break;
85+
case "vectorSearch":
86+
indexes = await provider.createSearchIndexes(database, collection, [
87+
{
88+
name,
89+
definition: {
90+
fields: definition.fields,
91+
},
92+
type: "vectorSearch",
93+
},
94+
]);
95+
break;
96+
}
3197

3298
return {
3399
content: [

tests/accuracy/createIndex.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ describeAccuracyTests([
1111
database: "mflix",
1212
collection: "movies",
1313
name: Matcher.anyOf(Matcher.undefined, Matcher.string()),
14-
keys: {
15-
release_year: 1,
14+
definition: {
15+
type: "classic",
16+
keys: {
17+
release_year: 1,
18+
},
1619
},
1720
},
1821
},
@@ -27,8 +30,11 @@ describeAccuracyTests([
2730
database: "mflix",
2831
collection: "movies",
2932
name: Matcher.anyOf(Matcher.undefined, Matcher.string()),
30-
keys: {
31-
title: "text",
33+
definition: {
34+
type: "classic",
35+
keys: {
36+
title: "text",
37+
},
3238
},
3339
},
3440
},

0 commit comments

Comments
 (0)