|
6 | 6 |
|
7 | 7 | from pymongo.collection import Collection |
8 | 8 | from pymongo.operations import SearchIndexModel |
| 9 | +from pymongo_search_utils import ( |
| 10 | + create_vector_search_index, # noqa: F401 |
| 11 | + drop_vector_search_index, # noqa: F401 |
| 12 | + update_vector_search_index, # noqa: F401 |
| 13 | +) |
9 | 14 |
|
10 | 15 | logger = logging.getLogger(__file__) |
11 | 16 |
|
@@ -34,135 +39,6 @@ def _vector_search_index_definition( |
34 | 39 | return definition |
35 | 40 |
|
36 | 41 |
|
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 | | -def drop_vector_search_index( |
92 | | - collection: Collection, |
93 | | - index_name: str, |
94 | | - *, |
95 | | - wait_until_complete: Optional[float] = None, |
96 | | -) -> None: |
97 | | - """Drop a created vector search index |
98 | | -
|
99 | | - Args: |
100 | | - collection (Collection): MongoDB Collection with index to be dropped |
101 | | - index_name (str): Name of the MongoDB index |
102 | | - wait_until_complete (Optional[float]): If provided, number of seconds to wait |
103 | | - until search index is ready. |
104 | | - """ |
105 | | - logger.info( |
106 | | - "Dropping Search Index %s from Collection: %s", index_name, collection.name |
107 | | - ) |
108 | | - collection.drop_search_index(index_name) |
109 | | - if wait_until_complete: |
110 | | - _wait_for_predicate( |
111 | | - predicate=lambda: len(list(collection.list_search_indexes())) == 0, |
112 | | - err=f"Index {index_name} did not drop in {wait_until_complete}!", |
113 | | - timeout=wait_until_complete, |
114 | | - ) |
115 | | - logger.info("Vector Search index %s.%s dropped", collection.name, index_name) |
116 | | - |
117 | | - |
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 | 42 | def _is_index_ready(collection: Collection, index_name: str) -> bool: |
167 | 43 | """Check for the index name in the list of available search indexes to see if the |
168 | 44 | specified index is of status READY |
|
0 commit comments