|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | from time import monotonic, sleep |
5 | | -from typing import Any, Callable, Dict, List, Optional, Union |
| 5 | +from typing import Any, Callable, Dict, List, Optional |
6 | 6 |
|
7 | 7 | from pymongo.collection import Collection |
8 | | -from pymongo.operations import SearchIndexModel |
9 | 8 | from pymongo_search_utils import ( |
| 9 | + create_fulltext_search_index, # noqa: F401 |
10 | 10 | create_vector_search_index, # noqa: F401 |
11 | 11 | drop_vector_search_index, # noqa: F401 |
12 | 12 | update_vector_search_index, # noqa: F401 |
@@ -75,50 +75,3 @@ def _wait_for_predicate( |
75 | 75 | if monotonic() - start > timeout: |
76 | 76 | raise TimeoutError(err) |
77 | 77 | sleep(interval) |
78 | | - |
79 | | - |
80 | | -def create_fulltext_search_index( |
81 | | - collection: Collection, |
82 | | - index_name: str, |
83 | | - field: Union[str, List[str]], |
84 | | - *, |
85 | | - wait_until_complete: Optional[float] = None, |
86 | | - **kwargs: Any, |
87 | | -) -> None: |
88 | | - """Experimental Utility function to create an Atlas Search index |
89 | | -
|
90 | | - Args: |
91 | | - collection (Collection): MongoDB Collection |
92 | | - index_name (str): Name of Index |
93 | | - field (str): Field to index |
94 | | - wait_until_complete (Optional[float]): If provided, number of seconds to wait |
95 | | - until search index is ready |
96 | | - kwargs: Keyword arguments supplying any additional options to SearchIndexModel. |
97 | | - """ |
98 | | - logger.info("Creating Search Index %s on %s", index_name, collection.name) |
99 | | - |
100 | | - if collection.name not in collection.database.list_collection_names( |
101 | | - authorizedCollections=True |
102 | | - ): |
103 | | - collection.database.create_collection(collection.name) |
104 | | - |
105 | | - if isinstance(field, str): |
106 | | - fields_definition = {field: [{"type": "string"}]} |
107 | | - else: |
108 | | - fields_definition = {f: [{"type": "string"}] for f in field} |
109 | | - definition = {"mappings": {"dynamic": False, "fields": fields_definition}} |
110 | | - result = collection.create_search_index( |
111 | | - SearchIndexModel( |
112 | | - definition=definition, |
113 | | - name=index_name, |
114 | | - type="search", |
115 | | - **kwargs, |
116 | | - ) |
117 | | - ) |
118 | | - if wait_until_complete: |
119 | | - _wait_for_predicate( |
120 | | - predicate=lambda: _is_index_ready(collection, index_name), |
121 | | - err=f"{index_name=} did not complete in {wait_until_complete}!", |
122 | | - timeout=wait_until_complete, |
123 | | - ) |
124 | | - logger.info(result) |
0 commit comments