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