diff --git a/examples/mongodb/2_load_and_index.ts b/examples/mongodb/2_load_and_index.ts index 0d7a3a23f70c45dffb37b1b1feca03a2f27d48ac..cb71340811e5915be9f523c5c15f569d96125f78 100644 --- a/examples/mongodb/2_load_and_index.ts +++ b/examples/mongodb/2_load_and_index.ts @@ -45,6 +45,39 @@ async function loadAndIndex() { await client.close(); } -loadAndIndex(); +/** + * This method is document in https://www.mongodb.com/docs/atlas/atlas-search/create-index/#create-an-fts-index-programmatically + * But, while testing a 'CommandNotFound' error occurred, so we're not using this here. + */ +async function createSearchIndex() { + const client = new MongoClient(mongoUri); + const database = client.db(databaseName); + const collection = database.collection(vectorCollectionName); + + // define your Atlas Search index + const index = { + name: indexName, + definition: { + /* search index definition fields */ + mappings: { + dynamic: true, + fields: [ + { + type: "vector", + path: "embedding", + numDimensions: 1536, + similarity: "cosine", + }, + ], + }, + }, + }; + // run the helper method + const result = await collection.createSearchIndex(index); + console.log("Successfully created search index:", result); + await client.close(); +} + +loadAndIndex().catch(console.error); // you can't query your index yet because you need to create a vector search index in mongodb's UI now diff --git a/examples/mongodb/README.md b/examples/mongodb/README.md index 61c8b4886f54a366d5bb45434015d2eff0dfd013..065c572ff9bceb084e08329b8156b717d1347556 100644 --- a/examples/mongodb/README.md +++ b/examples/mongodb/README.md @@ -76,7 +76,7 @@ Now if all has gone well you should be able to log in to the Mongo Atlas UI and Now it's time to create the vector search index so that you can query the data. It's not yet possible to programmatically create a vector search index using the [`createIndex`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createIndex/) function, therefore we have to create one manually in the UI. -To do so, first, click the Search tab, and then click "Create Search Index": +To do so, first, click the 'Atlas Search' tab, and then click "Create Search Index":  @@ -88,16 +88,14 @@ Now under "database and collection" select `tiny_tweets_db` and within that sele ```json { - "mappings": { - "dynamic": true, - "fields": { - "embedding": { - "dimensions": 1536, - "similarity": "cosine", - "type": "knnVector" - } + "fields": [ + { + "type": "vector", + "path": "embedding", + "numDimensions": 1536, + "similarity": "cosine" } - } + ] } ``` diff --git a/examples/mongodb/docs/4_search_tab.png b/examples/mongodb/docs/4_search_tab.png index 497ade44d5c89fa59bbfb95f48b80749e0819257..9c6ae2b548b94a8ad42a1f6a4e12b8923ae646aa 100644 Binary files a/examples/mongodb/docs/4_search_tab.png and b/examples/mongodb/docs/4_search_tab.png differ diff --git a/examples/mongodb/docs/5_json_editor.png b/examples/mongodb/docs/5_json_editor.png index 20ef31a1bb5baabdd01d4cc78403c02ff4872ea5..fc5b3f0f01b258b2c7127024dd5a330e6e94cca3 100644 Binary files a/examples/mongodb/docs/5_json_editor.png and b/examples/mongodb/docs/5_json_editor.png differ