|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | from time import monotonic, sleep |
5 | | -from typing import Any, Callable, Dict, List, Optional |
| 5 | +from typing import Any, Callable, Dict, List, Optional, Union |
6 | 6 |
|
7 | 7 | from pymongo.collection import Collection |
| 8 | +from pymongo.operations import SearchIndexModel |
8 | 9 |
|
9 | 10 | # Don't break imports for modules that expect these functions |
10 | 11 | # to be in this module. |
@@ -40,60 +41,6 @@ def _vector_search_index_definition( |
40 | 41 | return definition |
41 | 42 |
|
42 | 43 |
|
43 | | -def create_vector_search_index( |
44 | | - collection: Collection, |
45 | | - index_name: str, |
46 | | - dimensions: int, |
47 | | - path: str, |
48 | | - similarity: str, |
49 | | - filters: Optional[List[str]] = None, |
50 | | - *, |
51 | | - wait_until_complete: Optional[float] = None, |
52 | | - **kwargs: Any, |
53 | | -) -> None: |
54 | | - """Experimental Utility function to create a vector search index |
55 | | -
|
56 | | - Args: |
57 | | - collection (Collection): MongoDB Collection |
58 | | - index_name (str): Name of Index |
59 | | - dimensions (int): Number of dimensions in embedding |
60 | | - path (str): field with vector embedding |
61 | | - similarity (str): The similarity score used for the index |
62 | | - filters (List[str]): Fields/paths to index to allow filtering in $vectorSearch |
63 | | - wait_until_complete (Optional[float]): If provided, number of seconds to wait |
64 | | - until search index is ready. |
65 | | - kwargs: Keyword arguments supplying any additional options to SearchIndexModel. |
66 | | - """ |
67 | | - logger.info("Creating Search Index %s on %s", index_name, collection.name) |
68 | | - |
69 | | - if collection.name not in collection.database.list_collection_names( |
70 | | - authorizedCollections=True |
71 | | - ): |
72 | | - collection.database.create_collection(collection.name) |
73 | | - |
74 | | - result = collection.create_search_index( |
75 | | - SearchIndexModel( |
76 | | - definition=_vector_search_index_definition( |
77 | | - dimensions=dimensions, |
78 | | - path=path, |
79 | | - similarity=similarity, |
80 | | - filters=filters, |
81 | | - **kwargs, |
82 | | - ), |
83 | | - name=index_name, |
84 | | - type="vectorSearch", |
85 | | - ) |
86 | | - ) |
87 | | - |
88 | | - if wait_until_complete: |
89 | | - _wait_for_predicate( |
90 | | - predicate=lambda: _is_index_ready(collection, index_name), |
91 | | - err=f"{index_name=} did not complete in {wait_until_complete}!", |
92 | | - timeout=wait_until_complete, |
93 | | - ) |
94 | | - logger.info(result) |
95 | | - |
96 | | - |
97 | 44 | def drop_vector_search_index( |
98 | 45 | collection: Collection, |
99 | 46 | index_name: str, |
@@ -121,54 +68,6 @@ def drop_vector_search_index( |
121 | 68 | logger.info("Vector Search index %s.%s dropped", collection.name, index_name) |
122 | 69 |
|
123 | 70 |
|
124 | | -def update_vector_search_index( |
125 | | - collection: Collection, |
126 | | - index_name: str, |
127 | | - dimensions: int, |
128 | | - path: str, |
129 | | - similarity: str, |
130 | | - filters: Optional[List[str]] = None, |
131 | | - *, |
132 | | - wait_until_complete: Optional[float] = None, |
133 | | - **kwargs: Any, |
134 | | -) -> None: |
135 | | - """Update a search index. |
136 | | -
|
137 | | - Replace the existing index definition with the provided definition. |
138 | | -
|
139 | | - Args: |
140 | | - collection (Collection): MongoDB Collection |
141 | | - index_name (str): Name of Index |
142 | | - dimensions (int): Number of dimensions in embedding |
143 | | - path (str): field with vector embedding |
144 | | - similarity (str): The similarity score used for the index. |
145 | | - filters (List[str]): Fields/paths to index to allow filtering in $vectorSearch |
146 | | - wait_until_complete (Optional[float]): If provided, number of seconds to wait |
147 | | - until search index is ready. |
148 | | - kwargs: Keyword arguments supplying any additional options to SearchIndexModel. |
149 | | - """ |
150 | | - logger.info( |
151 | | - "Updating Search Index %s from Collection: %s", index_name, collection.name |
152 | | - ) |
153 | | - collection.update_search_index( |
154 | | - name=index_name, |
155 | | - definition=_vector_search_index_definition( |
156 | | - dimensions=dimensions, |
157 | | - path=path, |
158 | | - similarity=similarity, |
159 | | - filters=filters, |
160 | | - **kwargs, |
161 | | - ), |
162 | | - ) |
163 | | - if wait_until_complete: |
164 | | - _wait_for_predicate( |
165 | | - predicate=lambda: _is_index_ready(collection, index_name), |
166 | | - err=f"Index {index_name} update did not complete in {wait_until_complete}!", |
167 | | - timeout=wait_until_complete, |
168 | | - ) |
169 | | - logger.info("Update succeeded") |
170 | | - |
171 | | - |
172 | 71 | def _is_index_ready(collection: Collection, index_name: str) -> bool: |
173 | 72 | """Check for the index name in the list of available search indexes to see if the |
174 | 73 | specified index is of status READY |
|
0 commit comments