-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-indexes.py
60 lines (49 loc) · 1.66 KB
/
create-indexes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Inspiration https://www.mongodb.com/developer/products/atlas/how-use-cohere-embeddings-rerank-modules-mongodb-atlas/#programmatically-create-vector-search-and-full-text-search-index
from allthethings.mongo import Database
from pymongo.operations import SearchIndexModel
database = Database()
collection = database.get_collection()
if collection.name not in collection.database.list_collection_names():
print("Creating empty collection so indexes can be created.")
collection.database.create_collection(collection.name)
def create_or_update_search_index(index_name, index_definition, index_type):
indexes = list(collection.list_search_indexes(index_name))
if len(indexes) == 0:
print(f'Creating search index: "{index_name}"')
index_model = SearchIndexModel(
definition=index_definition,
name=index_name,
type=index_type,
)
collection.create_search_index(model=index_model)
else:
print(f'Search index "{index_name}" already exists. Updating.')
collection.update_search_index(name=index_name, definition=index_definition)
create_or_update_search_index(
"vector_index",
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 768,
"similarity": "euclidean",
}
]
},
"vectorSearch",
)
create_or_update_search_index(
"url_index",
{
"mappings": {
"fields": {
"url": {
"type": "string",
},
},
}
},
"search",
)
print("Indexes created successfully!")