Skip to content

Commit 85384b2

Browse files
committed
Replace create_vector_search_index
Remove create_vector_search_index from langchain-mongodb and import from pymongo-search-utils.
1 parent 53bbe48 commit 85384b2

File tree

1 file changed

+3
-54
lines changed
  • libs/langchain-mongodb/langchain_mongodb

1 file changed

+3
-54
lines changed

libs/langchain-mongodb/langchain_mongodb/index.py

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from pymongo.collection import Collection
88
from pymongo.operations import SearchIndexModel
9+
from pymongo_search_utils import (
10+
create_vector_search_index, # noqa: F401 # type: ignore
11+
)
912

1013
logger = logging.getLogger(__file__)
1114

@@ -34,60 +37,6 @@ def _vector_search_index_definition(
3437
return definition
3538

3639

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-
9140
def drop_vector_search_index(
9241
collection: Collection,
9342
index_name: str,

0 commit comments

Comments
 (0)