@@ -100,11 +100,27 @@ export class AggregateTool extends MongoDBToolBase {
100100
101101 // Check if aggregate operation uses an index if enabled
102102 if ( this . config . indexCheck ) {
103- await checkIndexUsage ( provider , database , collection , "aggregate" , async ( ) => {
104- return provider
105- . aggregate ( database , collection , pipeline , { } , { writeConcern : undefined } )
106- . explain ( "queryPlanner" ) ;
103+ const [ usesVectorSearchIndex , indexName ] = await this . isVectorSearchIndexUsed ( {
104+ database ,
105+ collection,
106+ pipeline ,
107107 } ) ;
108+ switch ( usesVectorSearchIndex ) {
109+ case "not-vector-search-query" :
110+ await checkIndexUsage ( provider , database , collection , "aggregate" , async ( ) => {
111+ return provider
112+ . aggregate ( database , collection , pipeline , { } , { writeConcern : undefined } )
113+ . explain ( "queryPlanner" ) ;
114+ } ) ;
115+ break ;
116+ case "non-existent-index" :
117+ throw new MongoDBError (
118+ ErrorCodes . AtlasVectorSearchIndexNotFound ,
119+ `Could not find an index with name "${ indexName } " in namespace "${ database } .${ collection } ".`
120+ ) ;
121+ case "valid-index" :
122+ // nothing to do, everything is correct so ready to run the query
123+ }
108124 }
109125
110126 pipeline = await this . replaceRawValuesWithEmbeddingsIfNecessary ( {
@@ -294,6 +310,41 @@ export class AggregateTool extends MongoDBToolBase {
294310 return pipeline ;
295311 }
296312
313+ private async isVectorSearchIndexUsed ( {
314+ database,
315+ collection,
316+ pipeline,
317+ } : {
318+ database : string ;
319+ collection : string ;
320+ pipeline : Document [ ] ;
321+ } ) : Promise < [ "valid-index" | "non-existent-index" | "not-vector-search-query" , string ?] > {
322+ // check if the pipeline contains a $vectorSearch stage
323+ let usesVectorSearch = false ;
324+ let indexName : string = "default" ;
325+
326+ for ( const stage of pipeline ) {
327+ if ( "$vectorSearch" in stage ) {
328+ const { $vectorSearch : vectorSearchStage } = stage as z . infer < typeof VectorSearchStage > ;
329+ usesVectorSearch = true ;
330+ indexName = vectorSearchStage . index ;
331+ break ;
332+ }
333+ }
334+
335+ if ( ! usesVectorSearch ) {
336+ return [ "not-vector-search-query" ] ;
337+ }
338+
339+ const indexExists = await this . session . vectorSearchEmbeddingsManager . indexExists ( {
340+ database,
341+ collection,
342+ indexName,
343+ } ) ;
344+
345+ return [ indexExists ? "valid-index" : "non-existent-index" , indexName ] ;
346+ }
347+
297348 private generateMessage ( {
298349 aggResultsCount,
299350 documents,
0 commit comments