|
17 | 17 |
|
18 | 18 | namespace MongoDB;
|
19 | 19 |
|
| 20 | +use Countable; |
| 21 | +use Iterator; |
20 | 22 | use MongoDB\BSON\JavascriptInterface;
|
21 | 23 | use MongoDB\Driver\Cursor;
|
| 24 | +use MongoDB\Driver\Exception\CommandException; |
22 | 25 | use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
|
23 | 26 | use MongoDB\Driver\Manager;
|
24 | 27 | use MongoDB\Driver\ReadConcern;
|
|
36 | 39 | use MongoDB\Operation\Count;
|
37 | 40 | use MongoDB\Operation\CountDocuments;
|
38 | 41 | use MongoDB\Operation\CreateIndexes;
|
| 42 | +use MongoDB\Operation\CreateSearchIndexes; |
39 | 43 | use MongoDB\Operation\DeleteMany;
|
40 | 44 | use MongoDB\Operation\DeleteOne;
|
41 | 45 | use MongoDB\Operation\Distinct;
|
42 | 46 | use MongoDB\Operation\DropCollection;
|
43 | 47 | use MongoDB\Operation\DropEncryptedCollection;
|
44 | 48 | use MongoDB\Operation\DropIndexes;
|
| 49 | +use MongoDB\Operation\DropSearchIndex; |
45 | 50 | use MongoDB\Operation\EstimatedDocumentCount;
|
46 | 51 | use MongoDB\Operation\Explain;
|
47 | 52 | use MongoDB\Operation\Explainable;
|
|
53 | 58 | use MongoDB\Operation\InsertMany;
|
54 | 59 | use MongoDB\Operation\InsertOne;
|
55 | 60 | use MongoDB\Operation\ListIndexes;
|
| 61 | +use MongoDB\Operation\ListSearchIndexes; |
56 | 62 | use MongoDB\Operation\MapReduce;
|
57 | 63 | use MongoDB\Operation\RenameCollection;
|
58 | 64 | use MongoDB\Operation\ReplaceOne;
|
59 | 65 | use MongoDB\Operation\UpdateMany;
|
60 | 66 | use MongoDB\Operation\UpdateOne;
|
| 67 | +use MongoDB\Operation\UpdateSearchIndex; |
61 | 68 | use MongoDB\Operation\Watch;
|
62 | 69 |
|
63 | 70 | use function array_diff_key;
|
@@ -377,6 +384,64 @@ public function createIndexes(array $indexes, array $options = [])
|
377 | 384 | return $operation->execute($server);
|
378 | 385 | }
|
379 | 386 |
|
| 387 | + /** |
| 388 | + * Create an Atlas Search index for the collection. |
| 389 | + * Only available when used against a 7.0+ Atlas cluster. |
| 390 | + * |
| 391 | + * @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/ |
| 392 | + * @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/ |
| 393 | + * @param array|object $definition Atlas Search index mapping definition |
| 394 | + * @param array{name?: string, comment?: mixed} $options Command options |
| 395 | + * @return string The name of the created search index |
| 396 | + * @throws UnsupportedException if options are not supported by the selected server |
| 397 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 398 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 399 | + */ |
| 400 | + public function createSearchIndex($definition, array $options = []): string |
| 401 | + { |
| 402 | + $index = ['definition' => $definition]; |
| 403 | + if (isset($options['name'])) { |
| 404 | + $index['name'] = $options['name']; |
| 405 | + unset($options['name']); |
| 406 | + } |
| 407 | + |
| 408 | + $names = $this->createSearchIndexes([$index], $options); |
| 409 | + |
| 410 | + return current($names); |
| 411 | + } |
| 412 | + |
| 413 | + /** |
| 414 | + * Create one or more Atlas Search indexes for the collection. |
| 415 | + * Only available when used against a 7.0+ Atlas cluster. |
| 416 | + * |
| 417 | + * Each element in the $indexes array must have "definition" document and they may have a "name" string. |
| 418 | + * The name can be omitted for a single index, in which case a name will be the default. |
| 419 | + * For example: |
| 420 | + * |
| 421 | + * $indexes = [ |
| 422 | + * // Create a search index with the default name, on |
| 423 | + * ['definition' => ['mappings' => ['dynamic' => false, 'fields' => ['title' => ['type' => 'string']]]]], |
| 424 | + * // Create a named search index on all fields |
| 425 | + * ['name' => 'search_all', 'definition' => ['mappings' => ['dynamic' => true]]], |
| 426 | + * ]; |
| 427 | + * |
| 428 | + * @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/ |
| 429 | + * @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/ |
| 430 | + * @param list<array{name?: string, definition: array|object}> $indexes List of search index specifications |
| 431 | + * @param array{comment?: string} $options Command options |
| 432 | + * @return string[] The names of the created search indexes |
| 433 | + * @throws UnsupportedException if options are not supported by the selected server |
| 434 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 435 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 436 | + */ |
| 437 | + public function createSearchIndexes(array $indexes, array $options = []): array |
| 438 | + { |
| 439 | + $operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options); |
| 440 | + $server = select_server($this->manager, $options); |
| 441 | + |
| 442 | + return $operation->execute($server); |
| 443 | + } |
| 444 | + |
380 | 445 | /**
|
381 | 446 | * Deletes all documents matching the filter.
|
382 | 447 | *
|
@@ -554,6 +619,31 @@ public function dropIndexes(array $options = [])
|
554 | 619 | return $operation->execute($server);
|
555 | 620 | }
|
556 | 621 |
|
| 622 | + /** |
| 623 | + * Drop a single Atlas Search index in the collection. |
| 624 | + * Only available when used against a 7.0+ Atlas cluster. |
| 625 | + * |
| 626 | + * @param string $name Search index name |
| 627 | + * @param array{comment?: mixed} $options Additional options |
| 628 | + * @throws UnsupportedException if options are not supported by the selected server |
| 629 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 630 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 631 | + */ |
| 632 | + public function dropSearchIndex(string $name, array $options = []): void |
| 633 | + { |
| 634 | + $operation = new DropSearchIndex($this->databaseName, $this->collectionName, $name); |
| 635 | + $server = select_server($this->manager, $options); |
| 636 | + |
| 637 | + try { |
| 638 | + $operation->execute($server); |
| 639 | + } catch (CommandException $e) { |
| 640 | + // Suppress namespace not found errors for idempotency |
| 641 | + if ($e->getCode() !== 26) { |
| 642 | + throw $e; |
| 643 | + } |
| 644 | + } |
| 645 | + } |
| 646 | + |
557 | 647 | /**
|
558 | 648 | * Gets an estimated number of documents in the collection using the collection metadata.
|
559 | 649 | *
|
@@ -928,6 +1018,24 @@ public function listIndexes(array $options = [])
|
928 | 1018 | return $operation->execute($server);
|
929 | 1019 | }
|
930 | 1020 |
|
| 1021 | + /** |
| 1022 | + * Returns information for all Atlas Search indexes for the collection. |
| 1023 | + * Only available when used against a 7.0+ Atlas cluster. |
| 1024 | + * |
| 1025 | + * @param array{name?: string} $options Command options |
| 1026 | + * @return Countable&Iterator<array{id: string, name: string, status: string, queryable: bool, latestDefinition: array}> |
| 1027 | + * @throws InvalidArgumentException for parameter/option parsing errors |
| 1028 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 1029 | + * @see ListSearchIndexes::__construct() for supported options |
| 1030 | + */ |
| 1031 | + public function listSearchIndexes(array $options = []): Iterator |
| 1032 | + { |
| 1033 | + $operation = new ListSearchIndexes($this->databaseName, $this->collectionName, $options); |
| 1034 | + $server = select_server($this->manager, $options); |
| 1035 | + |
| 1036 | + return $operation->execute($server); |
| 1037 | + } |
| 1038 | + |
931 | 1039 | /**
|
932 | 1040 | * Executes a map-reduce aggregation on the collection.
|
933 | 1041 | *
|
@@ -1088,6 +1196,25 @@ public function updateOne($filter, $update, array $options = [])
|
1088 | 1196 | return $operation->execute($server);
|
1089 | 1197 | }
|
1090 | 1198 |
|
| 1199 | + /** |
| 1200 | + * Update a single Atlas Search index in the collection. |
| 1201 | + * Only available when used against a 7.0+ Atlas cluster. |
| 1202 | + * |
| 1203 | + * @param string $name Search index name |
| 1204 | + * @param array|object $definition Atlas Search index definition |
| 1205 | + * @param array{comment?: mixed} $options Command options |
| 1206 | + * @throws UnsupportedException if options are not supported by the selected server |
| 1207 | + * @throws InvalidArgumentException for parameter parsing errors |
| 1208 | + * @throws DriverRuntimeException for other driver errors (e.g. connection errors) |
| 1209 | + */ |
| 1210 | + public function updateSearchIndex(string $name, $definition, array $options = []): void |
| 1211 | + { |
| 1212 | + $operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options); |
| 1213 | + $server = select_server($this->manager, $options); |
| 1214 | + |
| 1215 | + $operation->execute($server); |
| 1216 | + } |
| 1217 | + |
1091 | 1218 | /**
|
1092 | 1219 | * Create a change stream for watching changes to the collection.
|
1093 | 1220 | *
|
|
0 commit comments