Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-4297 Allow passing arbitrary index options to create_search_index #1561

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to remove this default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll ask that question in mongodb/specifications#1541.

**kwargs,
) -> 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 = {}
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
Loading