Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f757fe3

Browse filesBrowse files
authored
PYTHON-4297 Allow passing arbitrary options to create_search_index/SearchIndexModel (mongodb#1561)
1 parent ec4cb3e commit f757fe3
Copy full SHA for f757fe3

File tree

Expand file treeCollapse file tree

4 files changed

+24
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+24
-10
lines changed
Open diff view settings
Collapse file

‎doc/changelog.rst‎

Copy file name to clipboardExpand all lines: doc/changelog.rst
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ PyMongo 4.7 brings a number of improvements including:
3939
- Added the :attr:`pymongo.monitoring.ConnectionCheckedOutEvent.duration`,
4040
:attr:`pymongo.monitoring.ConnectionCheckOutFailedEvent.duration`, and
4141
:attr:`pymongo.monitoring.ConnectionReadyEvent.duration` properties.
42+
- Added the ``type`` and ``kwargs`` arguments to :class:`~pymongo.operations.SearchIndexModel` to enable
43+
creating vector search indexes in MongoDB Atlas.
4244

4345

4446
Unavoidable breaking changes
Collapse file

‎pymongo/collection.py‎

Copy file name to clipboardExpand all lines: pymongo/collection.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ def create_search_index(
24102410
.. versionadded:: 4.5
24112411
"""
24122412
if not isinstance(model, SearchIndexModel):
2413-
model = SearchIndexModel(model["definition"], model.get("name"), model.get("type"))
2413+
model = SearchIndexModel(**model)
24142414
return self.create_search_indexes([model], session, comment, **kwargs)[0]
24152415

24162416
def create_search_indexes(
Collapse file

‎pymongo/operations.py‎

Copy file name to clipboardExpand all lines: pymongo/operations.py
+13-8Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -593,23 +593,28 @@ def __init__(
593593
definition: Mapping[str, Any],
594594
name: Optional[str] = None,
595595
type: Optional[str] = "search",
596+
**kwargs: Any,
596597
) -> None:
597598
"""Create a Search Index instance.
598599
599600
For use with :meth:`~pymongo.collection.Collection.create_search_index` and :meth:`~pymongo.collection.Collection.create_search_indexes`.
600601
601-
:param definition: - The definition for this index.
602-
:param name: - The name for this index, if present.
603-
604-
.. versionadded:: 4.5
602+
:param definition: The definition for this index.
603+
:param name: The name for this index, if present.
604+
:param type: The type for this index which defaults to "search". Alternative values include "vectorSearch".
605+
:param kwargs: Keyword arguments supplying any additional options.
605606
606607
.. note:: Search indexes require a MongoDB server version 7.0+ Atlas cluster.
608+
.. versionadded:: 4.5
609+
.. versionchanged:: 4.7
610+
Added the type and kwargs arguments.
607611
"""
612+
self.__document: dict[str, Any] = {}
608613
if name is not None:
609-
self.__document = dict(name=name, definition=definition)
610-
else:
611-
self.__document = dict(definition=definition)
612-
self.__document["type"] = type # type: ignore[assignment]
614+
self.__document["name"] = name
615+
self.__document["definition"] = definition
616+
self.__document["type"] = type
617+
self.__document.update(kwargs)
613618

614619
@property
615620
def document(self) -> Mapping[str, Any]:
Collapse file

‎test/test_index_management.py‎

Copy file name to clipboardExpand all lines: test/test_index_management.py
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from test import IntegrationTest, unittest
2727
from test.unified_format import generate_test_classes
28+
from test.utils import AllowListEventListener
2829

2930
from pymongo import MongoClient
3031
from pymongo.errors import OperationFailure
@@ -39,7 +40,8 @@ class TestCreateSearchIndex(IntegrationTest):
3940
def test_inputs(self):
4041
if not os.environ.get("TEST_INDEX_MANAGEMENT"):
4142
raise unittest.SkipTest("Skipping index management tests")
42-
client = MongoClient()
43+
listener = AllowListEventListener("createSearchIndexes")
44+
client = MongoClient(event_listeners=[listener])
4345
self.addCleanup(client.close)
4446
coll = client.test.test
4547
coll.drop()
@@ -55,6 +57,11 @@ def test_inputs(self):
5557
with self.assertRaises(OperationFailure):
5658
coll.create_search_index(model_kwargs)
5759

60+
listener.reset()
61+
with self.assertRaises(OperationFailure):
62+
coll.create_search_index({"definition": definition, "arbitraryOption": 1})
63+
self.assertIn("arbitraryOption", listener.events[0].command["indexes"][0])
64+
5865

5966
class TestSearchIndexProse(unittest.TestCase):
6067
@classmethod

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.