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