Skip to content

Commit 87a034d

Browse files
committed
Update createSearchIndex signature
1 parent 35349df commit 87a034d

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
@@ -402,15 +402,21 @@ public function createIndexes(array $indexes, array $options = [])
402402
*
403403
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
404404
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
405-
* @param array{name?: string, definition: array|object} $index Atlas Search index specification
406-
* @param array $options Command options
405+
* @param array|object $definition Atlas Search index mapping definition
406+
* @param array{name?: string} $options Command options
407407
* @return string The name of the created search index
408408
* @throws UnsupportedException if options are not supported by the selected server
409409
* @throws InvalidArgumentException for parameter/option parsing errors
410410
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
411411
*/
412-
public function createSearchIndex(array $index, array $options = []): string
412+
public function createSearchIndex($definition, array $options = []): string
413413
{
414+
$index = ['definition' => $definition];
415+
if (isset($options['name'])) {
416+
$index['name'] = $options['name'];
417+
unset($options['name']);
418+
}
419+
414420
$names = $this->createSearchIndexes([$index], $options);
415421

416422
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;
@@ -559,7 +558,12 @@ function (IndexInfo $info) {
559558
);
560559

561560
case 'createSearchIndex':
562-
return $collection->createSearchIndex((array) $args['model']);
561+
$options = [];
562+
if (isset($args['name'])) {
563+
$options['name'] = $args['name'];
564+
}
565+
566+
return $collection->createSearchIndex($args['model']->definition, $options);
563567

564568
case 'createSearchIndexes':
565569
return $collection->createSearchIndexes($args['models']);
@@ -579,7 +583,7 @@ function (IndexInfo $info) {
579583
return $collection->updateSearchIndex($args['name'], $args['definition']);
580584

581585
case 'listSearchIndexes':
582-
return $collection->listSearchIndexes($args['name'] ?? null, document_to_array($args['aggregationOptions'] ?? []));
586+
return $collection->listSearchIndexes($args['name'] ?? null, (array) ($args['aggregationOptions'] ?? []));
583587

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

0 commit comments

Comments
 (0)