Skip to content

Commit

Permalink
PYTHON-4297 Allow passing arbitrary options to create_search_index/Se…
Browse files Browse the repository at this point in the history
…archIndexModel (#1561)
  • Loading branch information
ShaneHarvey authored Mar 26, 2024
1 parent ec4cb3e commit f757fe3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ PyMongo 4.7 brings a number of improvements including:
- Added the :attr:`pymongo.monitoring.ConnectionCheckedOutEvent.duration`,
:attr:`pymongo.monitoring.ConnectionCheckOutFailedEvent.duration`, and
:attr:`pymongo.monitoring.ConnectionReadyEvent.duration` properties.
- Added the ``type`` and ``kwargs`` arguments to :class:`~pymongo.operations.SearchIndexModel` to enable
creating vector search indexes in MongoDB Atlas.


Unavoidable breaking changes
Expand Down
2 changes: 1 addition & 1 deletion pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ def create_search_index(
.. versionadded:: 4.5
"""
if not isinstance(model, SearchIndexModel):
model = SearchIndexModel(model["definition"], model.get("name"), model.get("type"))
model = SearchIndexModel(**model)
return self.create_search_indexes([model], session, comment, **kwargs)[0]

def create_search_indexes(
Expand Down
21 changes: 13 additions & 8 deletions pymongo/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,23 +593,28 @@ def __init__(
definition: Mapping[str, Any],
name: Optional[str] = None,
type: Optional[str] = "search",
**kwargs: Any,
) -> None:
"""Create a Search Index instance.
For use with :meth:`~pymongo.collection.Collection.create_search_index` and :meth:`~pymongo.collection.Collection.create_search_indexes`.
:param definition: - The definition for this index.
:param name: - The name for this index, if present.
.. versionadded:: 4.5
:param definition: The definition for this index.
:param name: The name for this index, if present.
:param type: The type for this index which defaults to "search". Alternative values include "vectorSearch".
:param kwargs: Keyword arguments supplying any additional options.
.. note:: Search indexes require a MongoDB server version 7.0+ Atlas cluster.
.. versionadded:: 4.5
.. versionchanged:: 4.7
Added the type and kwargs arguments.
"""
self.__document: dict[str, Any] = {}
if name is not None:
self.__document = dict(name=name, definition=definition)
else:
self.__document = dict(definition=definition)
self.__document["type"] = type # type: ignore[assignment]
self.__document["name"] = name
self.__document["definition"] = definition
self.__document["type"] = type
self.__document.update(kwargs)

@property
def document(self) -> Mapping[str, Any]:
Expand Down
9 changes: 8 additions & 1 deletion test/test_index_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from test import IntegrationTest, unittest
from test.unified_format import generate_test_classes
from test.utils import AllowListEventListener

from pymongo import MongoClient
from pymongo.errors import OperationFailure
Expand All @@ -39,7 +40,8 @@ class TestCreateSearchIndex(IntegrationTest):
def test_inputs(self):
if not os.environ.get("TEST_INDEX_MANAGEMENT"):
raise unittest.SkipTest("Skipping index management tests")
client = MongoClient()
listener = AllowListEventListener("createSearchIndexes")
client = MongoClient(event_listeners=[listener])
self.addCleanup(client.close)
coll = client.test.test
coll.drop()
Expand All @@ -55,6 +57,11 @@ def test_inputs(self):
with self.assertRaises(OperationFailure):
coll.create_search_index(model_kwargs)

listener.reset()
with self.assertRaises(OperationFailure):
coll.create_search_index({"definition": definition, "arbitraryOption": 1})
self.assertIn("arbitraryOption", listener.events[0].command["indexes"][0])


class TestSearchIndexProse(unittest.TestCase):
@classmethod
Expand Down

0 comments on commit f757fe3

Please sign in to comment.