Skip to content

Commit b386083

Browse files
committed
Update createSearchIndex signature
1 parent 9fc8678 commit b386083

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/Collection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,15 +389,21 @@ public function createIndexes(array $indexes, array $options = [])
389389
*
390390
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
391391
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
392-
* @param array{name?: string, definition: array|object} $index Atlas Search index specification
393-
* @param array $options Command options
392+
* @param array|object $definition Atlas Search index mapping definition
393+
* @param array{name?: string} $options Command options
394394
* @return string The name of the created search index
395395
* @throws UnsupportedException if options are not supported by the selected server
396396
* @throws InvalidArgumentException for parameter/option parsing errors
397397
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
398398
*/
399-
public function createSearchIndex(array $index, array $options = []): string
399+
public function createSearchIndex($definition, array $options = []): string
400400
{
401+
$index = ['definition' => $definition];
402+
if (isset($options['name'])) {
403+
$index['name'] = $options['name'];
404+
unset($options['name']);
405+
}
406+
401407
$names = $this->createSearchIndexes([$index], $options);
402408

403409
return current($names);

src/Operation/UpdateSearchIndex.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
/**
3030
* Operation for the createIndexes command.
3131
*
32-
* @see \MongoDB\Collection::createSearchIndex()
33-
* @see \MongoDB\Collection::createSearchIndexes()
34-
* @see https://mongodb.com/docs/manual/reference/command/createSearchIndexes/
32+
* @see \MongoDB\Collection::updateSearchIndexes()
33+
* @see https://mongodb.com/docs/manual/reference/command/updateSearchIndexes/
3534
*/
3635
class UpdateSearchIndex implements Executable
3736
{

tests/Collection/SearchIndexFunctionalTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ public function testCreateSearchIndexesWithEmptyList(): void
4848
public function testCreateSearchIndexWithDefaultName(): void
4949
{
5050
$collection = $this->createCollection($this->getDatabaseName(), $this->getCollectionName());
51-
$name = $collection->createSearchIndex(['definition' => ['mappings' => ['dynamic' => true]]]);
51+
$name = $collection->createSearchIndex(['mappings' => ['dynamic' => true]]);
5252

5353
$this->assertSame('default', $name);
5454
}
5555

5656
public function testIndexLifecycle(): void
5757
{
5858
$collection = $this->createCollection($this->getDatabaseName(), $this->getCollectionName());
59-
60-
$name = 'search_index_' . bin2hex(random_bytes(5));
59+
$name = 'search_index';
6160

6261
// Create a search index
63-
$createdName = $collection->createSearchIndex(['name' => $name, 'definition' => ['mappings' => ['dynamic' => true]]]);
62+
$createdName = $collection->createSearchIndex(['mappings' => ['dynamic' => true]], ['name' => $name]);
6463
$this->assertSame($name, $createdName);
6564

6665
// Wait for the index to be ready
@@ -84,6 +83,7 @@ public function testIndexLifecycle(): void
8483
// Delete the search index
8584
$collection->dropSearchIndex($name);
8685

86+
// Wait for the index to be deleted
8787
$count = 0;
8888
do {
8989
sleep(1);
@@ -97,8 +97,8 @@ public function testIndexLifecycle(): void
9797
}
9898

9999
/**
100-
* Randomize the collection name to avoid duplicate index names when Atlas takes several seconds to be cleaned up
101-
* the indexes when their parent collection is dropped.
100+
* Randomize the collection name to avoid duplicate index names when running tests concurrently.
101+
* Search index operations are asynchronous and can take up to 1 minute.
102102
*/
103103
protected function getCollectionName(): string
104104
{

tests/UnifiedSpecTests/Operation.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
use function hex2bin;
3737
use function iterator_to_array;
3838
use function key;
39-
use function MongoDB\document_to_array;
4039
use function MongoDB\with_transaction;
4140
use function PHPUnit\Framework\assertArrayHasKey;
4241
use function PHPUnit\Framework\assertContains;
@@ -543,7 +542,12 @@ private function executeForCollection(Collection $collection)
543542
);
544543

545544
case 'createSearchIndex':
546-
return $collection->createSearchIndex((array) $args['model']);
545+
$options = [];
546+
if (isset($args['name'])) {
547+
$options['name'] = $args['name'];
548+
}
549+
550+
return $collection->createSearchIndex($args['model']->definition, $options);
547551

548552
case 'createSearchIndexes':
549553
return $collection->createSearchIndexes($args['models']);
@@ -563,7 +567,7 @@ private function executeForCollection(Collection $collection)
563567
return $collection->updateSearchIndex($args['name'], $args['definition']);
564568

565569
case 'listSearchIndexes':
566-
return $collection->listSearchIndexes($args['name'] ?? null, document_to_array($args['aggregationOptions'] ?? []));
570+
return $collection->listSearchIndexes($args['name'] ?? null, (array) ($args['aggregationOptions'] ?? []));
567571

568572
default:
569573
Assert::fail('Unsupported collection operation: ' . $this->name);

0 commit comments

Comments
 (0)