@@ -25,44 +25,56 @@ export async function createIndex(config) {
2525 console . log ( chalk . blue ( "🔍 Existing indexes:" ) , existingIndexes ) ;
2626
2727 await client . close ( ) ;
28- throw error ;
28+ throw new Error ( "createSearchIndexes() is not available" ) ;
2929 }
3030
3131 if ( ! config || ! config . embedding || ! config . embedding . dimensions ) {
3232 console . error ( chalk . red ( "❌ MongoDB Error: Missing embedding dimensions in config." ) ) ;
33- throw new Error ( "Missing embedding dimensions in config." ) ; // ✅ Instead of process.exit(1)
33+ throw new Error ( "Missing embedding dimensions in config." ) ;
3434 }
3535
3636 console . log ( chalk . blue ( `📌 Creating Vector Search Index: ${ config . indexName } ...` ) ) ;
3737
38- const indexDefinition = {
38+ const indexConfig = {
3939 name : config . indexName || "vector_index" ,
4040 definition : {
4141 mappings : {
42- dynamic : false , // ✅ Fix: Set dynamic to false to explicitly define the index
42+ dynamic : false , // More restrictive than true, better for vector search
4343 fields : {
4444 [ config . embedding . path || "embedding" ] : {
45- type : "vector " ,
46- numDimensions : config . embedding . dimensions || 1536 , // ✅ Ensure dimensions are set
45+ type : "knnVector " ,
46+ dimensions : config . embedding . dimensions ,
4747 similarity : config . embedding . similarity || "cosine"
4848 }
4949 }
5050 }
5151 }
5252 } ;
53-
5453
55- console . log ( chalk . blue ( `🔍 Debug: Index definition: ` , JSON . stringify ( indexDefinition , null , 2 ) ) ) ;
54+ console . log ( chalk . blue ( `🔍 Debug: Index definition: ` ) , JSON . stringify ( indexConfig , null , 2 ) ) ;
5655
57- const indexResult = await collection . createSearchIndexes ( [ indexDefinition ] ) ;
56+ try {
57+ const indexResult = await collection . createSearchIndex ( indexConfig ) ;
58+ console . log ( chalk . green ( `✅ Vector Search Index "${ indexConfig . name } " created successfully!` ) ) ;
59+ console . log ( chalk . blue ( `🔍 Index creation result:` ) , indexResult ) ;
60+
61+ await client . close ( ) ;
62+ console . log ( chalk . blue ( "🔍 MongoDB connection closed." ) ) ;
63+
64+ return {
65+ success : true ,
66+ message : "Vector search index created successfully" ,
67+ indexName : indexConfig . name ,
68+ result : indexResult
69+ } ;
70+ } catch ( error ) {
71+ console . error ( chalk . red ( `❌ Error creating search index: ${ error . message } ` ) ) ;
72+ await client . close ( ) ;
73+ throw error ;
74+ }
5875
59- console . log ( chalk . green ( `✅ Vector Search Index "${ config . indexName } " created successfully!` ) ) ;
60- console . log ( chalk . blue ( `🔍 Index creation result:` ) , indexResult ) ;
61- return indexResult ;
62- await client . close ( ) ;
63- console . log ( chalk . blue ( "🔍 MongoDB connection closed." ) ) ;
6476 } catch ( error ) {
65- console . error ( chalk . red ( `❌ MongoDB Error: ${ error . message } ` ) ) ;
66- throw error ;
77+ console . error ( chalk . red ( `❌ Error: ${ error . message } ` ) ) ;
78+ throw error ;
6779 }
68- }
80+ }
0 commit comments