Skip to content

Commit 6a0b5d5

Browse files
committed
-
1 parent d29ab3b commit 6a0b5d5

9 files changed

+421
-73
lines changed

src/Collection.php

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
use MongoDB\Operation\ReplaceOne;
6262
use MongoDB\Operation\UpdateMany;
6363
use MongoDB\Operation\UpdateOne;
64+
use MongoDB\Operation\UpdateSearchIndex;
6465
use MongoDB\Operation\Watch;
6566
use Traversable;
6667

@@ -394,45 +395,50 @@ public function createIndexes(array $indexes, array $options = [])
394395
}
395396

396397
/**
397-
* @param string $name
398-
* @param array|object $definition
399-
* @param array $options
398+
* Create an Atlas Search index for the collection.
399+
*
400+
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
401+
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
402+
* @param string $name List of search index specifications
403+
* @param array $definition Atlas Search index definition
404+
* @param array $options Command options
400405
* @return string The name of the created search index
406+
* @throws UnsupportedException if options are not supported by the selected server
407+
* @throws InvalidArgumentException for parameter/option parsing errors
408+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
401409
*/
402-
public function createSearchIndex(string $name, $definition, array $options = []): string
410+
public function createSearchIndex(string $name, array $definition, array $options): string
403411
{
404-
$server = select_server($this->manager, $options);
405-
406-
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
407-
$options['writeConcern'] = $this->writeConcern;
408-
}
409-
410-
$indexes = [
411-
[
412-
'name' => $name,
413-
'definition' => $definition,
414-
],
415-
];
412+
$names = $this->createSearchIndexes([['name' => $name, 'definition' => $definition]], $options);
416413

417-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
418-
419-
return current($operation->execute($server));
414+
return current($names);
420415
}
421416

422417
/**
423-
* @param array $indexes
424-
* @param array $options
425-
* @return string[]
418+
* Create one or more Atlas Search indexes for the collection.
419+
*
420+
* Each element in the $indexes array must have a "name" and a "definition" document.
421+
* For example:
422+
*
423+
* $indexes = [
424+
* // Create a 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 array[] $indexes List of search index specifications
431+
* @param array $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)
426436
*/
427437
public function createSearchIndexes(array $indexes, array $options = []): array
428438
{
429439
$server = select_server($this->manager, $options);
430440

431-
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
432-
$options['writeConcern'] = $this->writeConcern;
433-
}
434-
435-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
441+
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
436442

437443
return $operation->execute($server);
438444
}
@@ -615,24 +621,21 @@ public function dropIndexes(array $options = [])
615621
}
616622

617623
/**
618-
* Drop a single search index in the collection.
624+
* Drop a single Atlas Search index in the collection.
619625
*
620-
* @param string|IndexInfo $indexName Index name or model object
621-
* @param array $options Additional options
622-
* @return array|object Command result document
626+
* @param string $name Search index name
627+
* @param array $options Additional options
623628
* @throws UnsupportedException if options are not supported by the selected server
624629
* @throws InvalidArgumentException for parameter/option parsing errors
625630
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
626631
*/
627-
public function dropSearchIndex(string $indexName, array $options = [])
632+
public function dropSearchIndex(string $name, array $options = []): void
628633
{
629-
$indexName = (string) $indexName;
630-
631634
$server = select_server($this->manager, $options);
632635

633-
$operation = new DropSearchIndex($this->databaseName, $this->collectionName, $indexName);
636+
$operation = new DropSearchIndex($this->databaseName, $this->collectionName, $name);
634637

635-
return $operation->execute($server);
638+
$operation->execute($server);
636639
}
637640

638641
/**
@@ -1185,6 +1188,24 @@ public function updateOne($filter, $update, array $options = [])
11851188
return $operation->execute($server);
11861189
}
11871190

1191+
/**
1192+
* Update a single Atlas Search index in the collection.
1193+
*
1194+
* @param string $name Search index name
1195+
* @param array $definition Atlas Search index definition
1196+
* @param array $options Command options
1197+
* @throws UnsupportedException if options are not supported by the selected server
1198+
* @throws InvalidArgumentException for parameter parsing errors
1199+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
1200+
*/
1201+
public function updateSearchIndex(string $name, array $definition, array $options = []): void
1202+
{
1203+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
1204+
$server = select_server($this->manager, $options);
1205+
1206+
$operation->execute($server);
1207+
}
1208+
11881209
/**
11891210
* Create a change stream for watching changes to the collection.
11901211
*

src/Model/SearchIndexInfo.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
/*
3+
* Copyright 2015-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB\Model;
19+
20+
use ArrayAccess;
21+
use MongoDB\Exception\BadMethodCallException;
22+
use ReturnTypeWillChange;
23+
24+
use function array_key_exists;
25+
26+
/**
27+
* Index information model class.
28+
*
29+
* This class models the index information returned by the listIndexes command
30+
* or, for legacy servers, queries on the "system.indexes" collection. It
31+
* provides methods to access common index options, and allows access to other
32+
* options through the ArrayAccess interface (write methods are not supported).
33+
* For information on keys and index options, see the referenced
34+
* db.collection.createIndex() documentation.
35+
*
36+
* @see \MongoDB\Collection::listIndexes()
37+
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst
38+
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createIndex/
39+
*/
40+
class SearchIndexInfo implements ArrayAccess
41+
{
42+
/** @var array */
43+
private $info;
44+
45+
/** @param array $info Search index info */
46+
public function __construct(array $info)
47+
{
48+
$this->info = $info;
49+
}
50+
51+
/**
52+
* Return the collection info as an array.
53+
*
54+
* @see https://php.net/oop5.magic#language.oop5.magic.debuginfo
55+
* @return array
56+
*/
57+
public function __debugInfo()
58+
{
59+
return $this->info;
60+
}
61+
62+
/**
63+
* Return the index name to allow casting SearchIndexInfo to string.
64+
*
65+
* @return string
66+
*/
67+
public function __toString()
68+
{
69+
return $this->getName();
70+
}
71+
72+
/**
73+
* Return the index id.
74+
*/
75+
public function getId(): string
76+
{
77+
return (string) $this->info['id'];
78+
}
79+
80+
/**
81+
* Return the index name.
82+
*/
83+
public function getName(): string
84+
{
85+
return (string) $this->info['name'];
86+
}
87+
88+
/**
89+
* Return the index namespace (e.g. "db.collection").
90+
*/
91+
public function getNamespace(): string
92+
{
93+
return (string) $this->info['ns'];
94+
}
95+
96+
/**
97+
* Check whether a field exists in the index information.
98+
*
99+
* @see https://php.net/arrayaccess.offsetexists
100+
* @param mixed $key
101+
* @return boolean
102+
*/
103+
#[ReturnTypeWillChange]
104+
public function offsetExists($key)
105+
{
106+
return array_key_exists($key, $this->info);
107+
}
108+
109+
/**
110+
* Return the field's value from the search index information.
111+
*
112+
* This method satisfies the Enumerating Indexes specification's requirement
113+
* that index fields be made accessible under their original names. It may
114+
* also be used to access fields that do not have a helper method.
115+
*
116+
* @see https://php.net/arrayaccess.offsetget
117+
* @see https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst#getting-full-index-information
118+
* @param mixed $key
119+
* @return mixed
120+
*/
121+
#[ReturnTypeWillChange]
122+
public function offsetGet($key)
123+
{
124+
return $this->info[$key];
125+
}
126+
127+
/**
128+
* Not supported.
129+
*
130+
* @see https://php.net/arrayaccess.offsetset
131+
* @param mixed $key
132+
* @param mixed $value
133+
* @throws BadMethodCallException
134+
* @return void
135+
*/
136+
#[ReturnTypeWillChange]
137+
public function offsetSet($key, $value)
138+
{
139+
throw BadMethodCallException::classIsImmutable(self::class);
140+
}
141+
142+
/**
143+
* Not supported.
144+
*
145+
* @see https://php.net/arrayaccess.offsetunset
146+
* @param mixed $key
147+
* @throws BadMethodCallException
148+
* @return void
149+
*/
150+
#[ReturnTypeWillChange]
151+
public function offsetUnset($key)
152+
{
153+
throw BadMethodCallException::classIsImmutable(self::class);
154+
}
155+
}

src/Model/SearchIndexInfoIterator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
* Copyright 2015-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB\Model;
19+
20+
use Iterator;
21+
use ReturnTypeWillChange;
22+
23+
/**
24+
* SearchIndexInfoIterator interface.
25+
*
26+
* This iterator is used for enumerating search indexes in a collection.
27+
*
28+
* @see \MongoDB\Collection::listSearchIndexes()
29+
*/
30+
interface SearchIndexInfoIterator extends Iterator
31+
{
32+
/**
33+
* Return the current element as a IndexInfo instance.
34+
*
35+
* @return IndexInfo
36+
*/
37+
#[ReturnTypeWillChange]
38+
public function current();
39+
}

0 commit comments

Comments
 (0)