Skip to content

Commit 6dc2dd8

Browse files
committed
clean up type definitions
1 parent b08f5b1 commit 6dc2dd8

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

src/helpers/assertVectorSearchFilterFieldsAreIndexed.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { type CompositeLogger, LogId } from "../common/logger.js";
88
// https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/#mongodb-vector-search-pre-filter
99
const ALLOWED_LOGICAL_OPERATORS = ["$not", "$nor", "$and", "$or"];
1010

11-
export type VectorSearchIndex = {
11+
export type SearchIndex = VectorSearchIndex | AtlasSearchIndex;
12+
13+
type VectorSearchIndex = {
1214
name: string;
1315
latestDefinition: {
1416
fields: Array<
@@ -24,16 +26,22 @@ export type VectorSearchIndex = {
2426
type: "vectorSearch";
2527
};
2628

29+
type AtlasSearchIndex = {
30+
name: string;
31+
latestDefinition: unknown;
32+
type: "search";
33+
};
34+
2735
export function assertVectorSearchFilterFieldsAreIndexed({
28-
vectorSearchIndexes,
36+
searchIndexes,
2937
pipeline,
3038
logger,
3139
}: {
32-
vectorSearchIndexes: VectorSearchIndex[];
40+
searchIndexes: SearchIndex[];
3341
pipeline: Record<string, unknown>[];
3442
logger: CompositeLogger;
3543
}): void {
36-
const searchIndexesWithFilterFields = vectorSearchIndexes
44+
const searchIndexesWithFilterFields = searchIndexes
3745
// Ensure we only process vector search indexes and not lexical search ones
3846
.filter((index) => index.type === "vectorSearch")
3947
.reduce<Record<string, string[]>>((indexFieldMap, searchIndex) => {

src/tools/mongodb/read/aggregate.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { LogId } from "../../../common/logger.js";
1515
import { AnyVectorSearchStage, VectorSearchStage } from "../mongodbSchemas.js";
1616
import {
1717
assertVectorSearchFilterFieldsAreIndexed,
18-
type VectorSearchIndex,
18+
type SearchIndex,
1919
} from "../../../helpers/assertVectorSearchFilterFieldsAreIndexed.js";
2020

2121
export const AggregateArgs = {
@@ -59,9 +59,7 @@ export class AggregateTool extends MongoDBToolBase {
5959
await this.assertOnlyUsesPermittedStages(pipeline);
6060
if (await this.session.isSearchSupported()) {
6161
assertVectorSearchFilterFieldsAreIndexed({
62-
vectorSearchIndexes: (await provider.getSearchIndexes(database, collection)).filter(
63-
(index) => index.type === "vectorSearch"
64-
) as VectorSearchIndex[],
62+
searchIndexes: (await provider.getSearchIndexes(database, collection)) as SearchIndex[],
6563
pipeline,
6664
logger: this.session.logger,
6765
});

tests/unit/helpers/assertVectorSearchFilterFieldsAreIndexed.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";
22
import {
33
assertVectorSearchFilterFieldsAreIndexed,
44
collectFieldsFromVectorSearchFilter,
5-
type VectorSearchIndex,
5+
type SearchIndex,
66
} from "../../../src/helpers/assertVectorSearchFilterFieldsAreIndexed.js";
77
import { ErrorCodes, MongoDBError } from "../../../src/common/errors.js";
88
import { type CompositeLogger, LogId } from "../../../src/common/logger.js";
@@ -184,7 +184,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
184184
error: vi.fn(),
185185
} as unknown as CompositeLogger;
186186

187-
const createMockSearchIndexes = (indexName: string, filterFields: string[]): VectorSearchIndex[] => [
187+
const createMockSearchIndexes = (indexName: string, filterFields: string[]): SearchIndex[] => [
188188
{
189189
name: indexName,
190190
latestDefinition: {
@@ -201,7 +201,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
201201
];
202202

203203
it("should not throw when all filter fields are indexed", () => {
204-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2", "field3"]);
204+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2", "field3"]);
205205
const pipeline = [
206206
{
207207
$vectorSearch: {
@@ -220,15 +220,15 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
220220

221221
expect(() =>
222222
assertVectorSearchFilterFieldsAreIndexed({
223-
vectorSearchIndexes,
223+
searchIndexes,
224224
pipeline,
225225
logger: mockLogger,
226226
})
227227
).not.toThrow();
228228
});
229229

230230
it("should not throw when filter is empty", () => {
231-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
231+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
232232
const pipeline = [
233233
{
234234
$vectorSearch: {
@@ -244,15 +244,15 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
244244

245245
expect(() =>
246246
assertVectorSearchFilterFieldsAreIndexed({
247-
vectorSearchIndexes,
247+
searchIndexes,
248248
pipeline,
249249
logger: mockLogger,
250250
})
251251
).not.toThrow();
252252
});
253253

254254
it("should not throw when filter is not provided", () => {
255-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
255+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
256256
const pipeline = [
257257
{
258258
$vectorSearch: {
@@ -267,28 +267,28 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
267267

268268
expect(() =>
269269
assertVectorSearchFilterFieldsAreIndexed({
270-
vectorSearchIndexes,
270+
searchIndexes,
271271
pipeline,
272272
logger: mockLogger,
273273
})
274274
).not.toThrow();
275275
});
276276

277277
it("should not throw when pipeline has no $vectorSearch stage", () => {
278-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
278+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
279279
const pipeline = [{ $match: { status: "active" } }, { $limit: 10 }];
280280

281281
expect(() =>
282282
assertVectorSearchFilterFieldsAreIndexed({
283-
vectorSearchIndexes,
283+
searchIndexes,
284284
pipeline,
285285
logger: mockLogger,
286286
})
287287
).not.toThrow();
288288
});
289289

290290
it("should throw MongoDBError when filter field is not indexed", () => {
291-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2"]);
291+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2"]);
292292
const pipeline = [
293293
{
294294
$vectorSearch: {
@@ -307,15 +307,15 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
307307

308308
expect(() =>
309309
assertVectorSearchFilterFieldsAreIndexed({
310-
vectorSearchIndexes,
310+
searchIndexes,
311311
pipeline,
312312
logger: mockLogger,
313313
})
314314
).toThrow(MongoDBError);
315315

316316
expect(() =>
317317
assertVectorSearchFilterFieldsAreIndexed({
318-
vectorSearchIndexes,
318+
searchIndexes,
319319
pipeline,
320320
logger: mockLogger,
321321
})
@@ -328,7 +328,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
328328
});
329329

330330
it("should throw MongoDBError with all unindexed fields listed", () => {
331-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
331+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
332332
const pipeline = [
333333
{
334334
$vectorSearch: {
@@ -348,7 +348,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
348348

349349
expect(() =>
350350
assertVectorSearchFilterFieldsAreIndexed({
351-
vectorSearchIndexes,
351+
searchIndexes,
352352
pipeline,
353353
logger: mockLogger,
354354
})
@@ -361,7 +361,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
361361
});
362362

363363
it("should handle nested $and and $or operators", () => {
364-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2", "field3"]);
364+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2", "field3"]);
365365
const pipeline = [
366366
{
367367
$vectorSearch: {
@@ -384,15 +384,15 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
384384

385385
expect(() =>
386386
assertVectorSearchFilterFieldsAreIndexed({
387-
vectorSearchIndexes,
387+
searchIndexes,
388388
pipeline,
389389
logger: mockLogger,
390390
})
391391
).not.toThrow();
392392
});
393393

394394
it("should throw when nested filter contains unindexed field", () => {
395-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2"]);
395+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1", "field2"]);
396396
const pipeline = [
397397
{
398398
$vectorSearch: {
@@ -415,7 +415,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
415415

416416
expect(() =>
417417
assertVectorSearchFilterFieldsAreIndexed({
418-
vectorSearchIndexes,
418+
searchIndexes,
419419
pipeline,
420420
logger: mockLogger,
421421
})
@@ -428,7 +428,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
428428
});
429429

430430
it("should log warning when index is not found in searchIndexes", () => {
431-
const vectorSearchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
431+
const searchIndexes = createMockSearchIndexes("myIndex", ["field1"]);
432432
const pipeline = [
433433
{
434434
$vectorSearch: {
@@ -445,7 +445,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
445445
];
446446

447447
assertVectorSearchFilterFieldsAreIndexed({
448-
vectorSearchIndexes,
448+
searchIndexes,
449449
pipeline,
450450
logger: mockLogger,
451451
});
@@ -460,7 +460,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
460460
});
461461

462462
it("should handle multiple $vectorSearch stages in pipeline", () => {
463-
const vectorSearchIndexes = [
463+
const searchIndexes = [
464464
...createMockSearchIndexes("index1", ["field1", "field2"]),
465465
...createMockSearchIndexes("index2", ["field3", "field4"]),
466466
];
@@ -494,15 +494,15 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
494494

495495
expect(() =>
496496
assertVectorSearchFilterFieldsAreIndexed({
497-
vectorSearchIndexes,
497+
searchIndexes,
498498
pipeline,
499499
logger: mockLogger,
500500
})
501501
).not.toThrow();
502502
});
503503

504504
it("should throw on second $vectorSearch stage if it has unindexed field", () => {
505-
const vectorSearchIndexes = [
505+
const searchIndexes = [
506506
...createMockSearchIndexes("index1", ["field1", "field2"]),
507507
...createMockSearchIndexes("index2", ["field3"]),
508508
];
@@ -535,7 +535,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
535535

536536
expect(() =>
537537
assertVectorSearchFilterFieldsAreIndexed({
538-
vectorSearchIndexes,
538+
searchIndexes,
539539
pipeline,
540540
logger: mockLogger,
541541
})
@@ -548,7 +548,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
548548
});
549549

550550
it("should handle search index with no filter fields", () => {
551-
const vectorSearchIndexes: VectorSearchIndex[] = [
551+
const searchIndexes: SearchIndex[] = [
552552
{
553553
name: "myIndex",
554554
latestDefinition: {
@@ -574,7 +574,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
574574

575575
expect(() =>
576576
assertVectorSearchFilterFieldsAreIndexed({
577-
vectorSearchIndexes,
577+
searchIndexes,
578578
pipeline,
579579
logger: mockLogger,
580580
})
@@ -587,7 +587,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
587587
});
588588

589589
it("should ignore atlas search indexes", () => {
590-
const vectorSearchIndexes: VectorSearchIndex[] = [
590+
const searchIndexes: SearchIndex[] = [
591591
...createMockSearchIndexes("index1", ["field1", "field2"]),
592592
// Atlas search index - it should be ignored by the validation
593593
// and not cause any errors
@@ -600,7 +600,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
600600
},
601601
},
602602
type: "search",
603-
} as unknown as VectorSearchIndex,
603+
},
604604
];
605605

606606
const pipeline = [
@@ -620,7 +620,7 @@ describe("#assertVectorSearchFilterFieldsAreIndexed", () => {
620620

621621
expect(() =>
622622
assertVectorSearchFilterFieldsAreIndexed({
623-
vectorSearchIndexes,
623+
searchIndexes: searchIndexes,
624624
pipeline,
625625
logger: mockLogger,
626626
})

0 commit comments

Comments
 (0)