Skip to content

Commit c95beec

Browse files
committed
Add script to create vector search index
1 parent cab9755 commit c95beec

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import pymongo
3+
import time
4+
import os
5+
import pprint
6+
7+
CONNECTION_STRING = os.environ.get("CONNECTION_STRING")
8+
database_client = pymongo.MongoClient(CONNECTION_STRING)
9+
collection = database_client["grocery_store"]["inventory"]
10+
11+
index_definition = {
12+
"name": "vector_index",
13+
"type": "vectorSearch",
14+
"definition": {
15+
"fields": [
16+
{
17+
"numDimensions": 768,
18+
"path": "gemini_embedding",
19+
"similarity": "cosine",
20+
"type": "vector"
21+
}
22+
]
23+
}
24+
}
25+
26+
print("Creating the vector search index...")
27+
collection.create_search_index(index_definition)
28+
29+
print("Waiting 10 seconds for the index to finish building...")
30+
time.sleep(10)
31+
32+
indexes = list(collection.list_search_indexes())
33+
print("Search indexes: ")
34+
pprint.pp(indexes)

0 commit comments

Comments
 (0)