|
17 | 17 |
|
18 | 18 | namespace MongoDB;
|
19 | 19 |
|
| 20 | +use Countable; |
20 | 21 | use Iterator;
|
21 | 22 | use MongoDB\BSON\JavascriptInterface;
|
22 | 23 | use MongoDB\Codec\DocumentCodec;
|
23 | 24 | use MongoDB\Driver\CursorInterface;
|
| 25 | +use MongoDB\Driver\Exception\CommandException; |
24 | 26 | use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
|
25 | 27 | use MongoDB\Driver\Manager;
|
26 | 28 | use MongoDB\Driver\ReadConcern;
|
|
38 | 40 | use MongoDB\Operation\Count;
|
39 | 41 | use MongoDB\Operation\CountDocuments;
|
40 | 42 | use MongoDB\Operation\CreateIndexes;
|
| 43 | +use MongoDB\Operation\CreateSearchIndexes; |
41 | 44 | use MongoDB\Operation\DeleteMany;
|
42 | 45 | use MongoDB\Operation\DeleteOne;
|
43 | 46 | use MongoDB\Operation\Distinct;
|
44 | 47 | use MongoDB\Operation\DropCollection;
|
45 | 48 | use MongoDB\Operation\DropEncryptedCollection;
|
46 | 49 | use MongoDB\Operation\DropIndexes;
|
| 50 | +use MongoDB\Operation\DropSearchIndex; |
47 | 51 | use MongoDB\Operation\EstimatedDocumentCount;
|
48 | 52 | use MongoDB\Operation\Explain;
|
49 | 53 | use MongoDB\Operation\Explainable;
|
|
55 | 59 | use MongoDB\Operation\InsertMany;
|
56 | 60 | use MongoDB\Operation\InsertOne;
|
57 | 61 | use MongoDB\Operation\ListIndexes;
|
| 62 | +use MongoDB\Operation\ListSearchIndexes; |
58 | 63 | use MongoDB\Operation\MapReduce;
|
59 | 64 | use MongoDB\Operation\RenameCollection;
|
60 | 65 | use MongoDB\Operation\ReplaceOne;
|
61 | 66 | use MongoDB\Operation\UpdateMany;
|
62 | 67 | use MongoDB\Operation\UpdateOne;
|
| 68 | +use MongoDB\Operation\UpdateSearchIndex; |
63 | 69 | use MongoDB\Operation\Watch;
|
64 | 70 |
|
65 | 71 | use function array_diff_key;
|
@@ -360,6 +366,64 @@ public function createIndexes(array $indexes, array $options = [])
|
360 | 366 | return $operation->execute(select_server($this->manager, $options));
|
361 | 367 | }
|
362 | 368 |
|
| 369 | + /** |
| 370 | + * Create an Atlas Search index for the collection. |
| 371 | + * Only available when used against a 7.0+ Atlas cluster. |
| 372 | + * |
| 373 | + * @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/ |
| 374 | + * @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/ |
| 375 | + * @param array|object $definition Atlas Search index mapping definition |
| 376 | + * @param array{name?: string, comment?: mixed} $options Command options |
| 377 | + * @return string The name of the created search index |
| 378 | + * @throws UnsupportedException if options are not supported by the selected server |
| 379 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 380 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 381 | + */ |
| 382 | + public function createSearchIndex($definition, array $options = []): string |
| 383 | + { |
| 384 | + $index = ['definition' => $definition]; |
| 385 | + if (isset($options['name'])) { |
| 386 | + $index['name'] = $options['name']; |
| 387 | + unset($options['name']); |
| 388 | + } |
| 389 | + |
| 390 | + $names = $this->createSearchIndexes([$index], $options); |
| 391 | + |
| 392 | + return current($names); |
| 393 | + } |
| 394 | + |
| 395 | + /** |
| 396 | + * Create one or more Atlas Search indexes for the collection. |
| 397 | + * Only available when used against a 7.0+ Atlas cluster. |
| 398 | + * |
| 399 | + * Each element in the $indexes array must have "definition" document and they may have a "name" string. |
| 400 | + * The name can be omitted for a single index, in which case a name will be the default. |
| 401 | + * For example: |
| 402 | + * |
| 403 | + * $indexes = [ |
| 404 | + * // Create a search index with the default name, on |
| 405 | + * ['definition' => ['mappings' => ['dynamic' => false, 'fields' => ['title' => ['type' => 'string']]]]], |
| 406 | + * // Create a named search index on all fields |
| 407 | + * ['name' => 'search_all', 'definition' => ['mappings' => ['dynamic' => true]]], |
| 408 | + * ]; |
| 409 | + * |
| 410 | + * @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/ |
| 411 | + * @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/ |
| 412 | + * @param list<array{name?: string, definition: array|object}> $indexes List of search index specifications |
| 413 | + * @param array{comment?: string} $options Command options |
| 414 | + * @return string[] The names of the created search indexes |
| 415 | + * @throws UnsupportedException if options are not supported by the selected server |
| 416 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 417 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 418 | + */ |
| 419 | + public function createSearchIndexes(array $indexes, array $options = []): array |
| 420 | + { |
| 421 | + $operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options); |
| 422 | + $server = select_server($this->manager, $options); |
| 423 | + |
| 424 | + return $operation->execute($server); |
| 425 | + } |
| 426 | + |
363 | 427 | /**
|
364 | 428 | * Deletes all documents matching the filter.
|
365 | 429 | *
|
@@ -501,6 +565,31 @@ public function dropIndexes(array $options = [])
|
501 | 565 | return $operation->execute(select_server($this->manager, $options));
|
502 | 566 | }
|
503 | 567 |
|
| 568 | + /** |
| 569 | + * Drop a single Atlas Search index in the collection. |
| 570 | + * Only available when used against a 7.0+ Atlas cluster. |
| 571 | + * |
| 572 | + * @param string $name Search index name |
| 573 | + * @param array{comment?: mixed} $options Additional options |
| 574 | + * @throws UnsupportedException if options are not supported by the selected server |
| 575 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 576 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 577 | + */ |
| 578 | + public function dropSearchIndex(string $name, array $options = []): void |
| 579 | + { |
| 580 | + $operation = new DropSearchIndex($this->databaseName, $this->collectionName, $name); |
| 581 | + $server = select_server($this->manager, $options); |
| 582 | + |
| 583 | + try { |
| 584 | + $operation->execute($server); |
| 585 | + } catch (CommandException $e) { |
| 586 | + // Suppress namespace not found errors for idempotency |
| 587 | + if ($e->getCode() !== 26) { |
| 588 | + throw $e; |
| 589 | + } |
| 590 | + } |
| 591 | + } |
| 592 | + |
504 | 593 | /**
|
505 | 594 | * Gets an estimated number of documents in the collection using the collection metadata.
|
506 | 595 | *
|
@@ -812,6 +901,24 @@ public function listIndexes(array $options = [])
|
812 | 901 | return $operation->execute(select_server($this->manager, $options));
|
813 | 902 | }
|
814 | 903 |
|
| 904 | + /** |
| 905 | + * Returns information for all Atlas Search indexes for the collection. |
| 906 | + * Only available when used against a 7.0+ Atlas cluster. |
| 907 | + * |
| 908 | + * @param array{name?: string} $options Command options |
| 909 | + * @return Countable&Iterator<array{id: string, name: string, status: string, queryable: bool, latestDefinition: array}> |
| 910 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 911 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 912 | + * @see ListSearchIndexes::__construct() for supported options |
| 913 | + */ |
| 914 | + public function listSearchIndexes(array $options = []): Iterator |
| 915 | + { |
| 916 | + $operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options); |
| 917 | + $server = select_server($this->manager, $options); |
| 918 | + |
| 919 | + return $operation->execute($server); |
| 920 | + } |
| 921 | + |
815 | 922 | /**
|
816 | 923 | * Executes a map-reduce aggregation on the collection.
|
817 | 924 | *
|
@@ -946,6 +1053,25 @@ public function updateOne($filter, $update, array $options = [])
|
946 | 1053 | return $operation->execute(select_server($this->manager, $options));
|
947 | 1054 | }
|
948 | 1055 |
|
| 1056 | + /** |
| 1057 | + * Update a single Atlas Search index in the collection. |
| 1058 | + * Only available when used against a 7.0+ Atlas cluster. |
| 1059 | + * |
| 1060 | + * @param string $name Search index name |
| 1061 | + * @param array|object $definition Atlas Search index definition |
| 1062 | + * @param array{comment?: mixed} $options Command options |
| 1063 | + * @throws UnsupportedException if options are not supported by the selected server |
| 1064 | + * @throws InvalidArgumentException for parameter parsing errors |
| 1065 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 1066 | + */ |
| 1067 | + public function updateSearchIndex(string $name, $definition, array $options = []): void |
| 1068 | + { |
| 1069 | + $operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options); |
| 1070 | + $server = select_server($this->manager, $options); |
| 1071 | + |
| 1072 | + $operation->execute($server); |
| 1073 | + } |
| 1074 | + |
949 | 1075 | /**
|
950 | 1076 | * Create a change stream for watching changes to the collection.
|
951 | 1077 | *
|
|
0 commit comments