Skip to content

PHPLIB-1548 Inherit typeMap option in Collection::listSearchIndexes() #1482

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

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ public function listIndexes(array $options = [])
*/
public function listSearchIndexes(array $options = []): Iterator
{
$options = $this->inheritTypeMap($options);

$operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options);
$server = select_server($this->manager, $options);

Expand Down
28 changes: 28 additions & 0 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Exception\CommandException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
Expand All @@ -23,6 +24,7 @@
use function array_filter;
use function call_user_func;
use function is_scalar;
use function iterator_to_array;
use function json_encode;
use function str_contains;
use function usort;
Expand Down Expand Up @@ -800,6 +802,32 @@ public function testMethodInTransactionWithReadConcernOption($method): void
}
}

public function testListSearchIndexesInheritTypeMap(): void
{
$this->skipIfAtlasSearchIndexIsNotSupported();

$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), ['typeMap' => ['root' => 'array']]);

// Insert a document to create the collection
$collection->insertOne(['_id' => 1]);

try {
$collection->createSearchIndex(['mappings' => ['dynamic' => false]], ['name' => 'test-search-index']);
Copy link
Member

@jmikola jmikola Oct 15, 2024

Choose a reason for hiding this comment

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

Does dynamic: false make sense here? Quoting createSearchIndexes command docs:

If set to false, you must specify individual fields to index using mappings.fields.

Presumably, this creates a search index that does nothing. I'm curious as to why the server would even allow that, but wonder if future validation might break this. dynamic: true seems more correct (if only as a valid demonstration).

Copy link
Member Author

@GromNaN GromNaN Oct 15, 2024

Choose a reason for hiding this comment

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

I used this mapping because it is in the spec test: https://github.com/mongodb/specifications/blob/b746fcc71ad7c1b376482852c3c9cbba4eed7d26/source/index-management/tests/README.md#case-1-driver-can-successfully-create-and-list-search-indexes

This requires a spec change if we want a client error in case of dynamic: false AND fields: empty

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for digging that up. Since spec test reads the definition back from the server, I think this goes beyond anything drivers should address themselves. I'll share a link to this thread with the Atlas Search team but OK to leave this PR as-is.

} catch (CommandException $e) {
// Ignore duplicate errors in case this test is re-run too quickly
// Index is asynchronously dropped during tearDown, we only need to
// ensure it exists for this test.
if ($e->getCode() !== 68 /* IndexAlreadyExists */) {
throw $e;
}
}

$indexes = $collection->listSearchIndexes();
$indexes = iterator_to_array($indexes);
$this->assertCount(1, $indexes);
$this->assertIsArray($indexes[0]);
}

/**
* Create data fixtures.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ protected function skipIfServerVersion(string $operator, string $version, ?strin
}
}

protected function skipIfAtlasSearchIndexIsNotSupported(): void
{
if (! self::isAtlas()) {
self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+');
}

$this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+');
}

protected function skipIfChangeStreamIsNotSupported(): void
{
if ($this->isStandalone()) {
Expand Down
6 changes: 1 addition & 5 deletions tests/SpecTests/SearchIndexSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ class SearchIndexSpecTest extends FunctionalTestCase

public function setUp(): void
{
if (! self::isAtlas()) {
self::markTestSkipped('Search Indexes are only supported on MongoDB Atlas 7.0+');
}

parent::setUp();

$this->skipIfServerVersion('<', '7.0', 'Search Indexes are only supported on MongoDB Atlas 7.0+');
$this->skipIfAtlasSearchIndexIsNotSupported();
}

/**
Expand Down