Skip to content

Commit a2bfd13

Browse files
committed
-
1 parent 1e6d405 commit a2bfd13

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

6667
use function array_diff_key;
@@ -381,45 +382,50 @@ public function createIndexes(array $indexes, array $options = [])
381382
}
382383

383384
/**
384-
* @param string $name
385-
* @param array|object $definition
386-
* @param array $options
385+
* Create an Atlas Search index for the collection.
386+
*
387+
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
388+
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
389+
* @param string $name List of search index specifications
390+
* @param array $definition Atlas Search index definition
391+
* @param array $options Command options
387392
* @return string The name of the created search index
393+
* @throws UnsupportedException if options are not supported by the selected server
394+
* @throws InvalidArgumentException for parameter/option parsing errors
395+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
388396
*/
389-
public function createSearchIndex(string $name, $definition, array $options = []): string
397+
public function createSearchIndex(string $name, array $definition, array $options): string
390398
{
391-
$server = select_server($this->manager, $options);
392-
393-
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
394-
$options['writeConcern'] = $this->writeConcern;
395-
}
396-
397-
$indexes = [
398-
[
399-
'name' => $name,
400-
'definition' => $definition,
401-
],
402-
];
399+
$names = $this->createSearchIndexes([['name' => $name, 'definition' => $definition]], $options);
403400

404-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
405-
406-
return current($operation->execute($server));
401+
return current($names);
407402
}
408403

409404
/**
410-
* @param array $indexes
411-
* @param array $options
412-
* @return string[]
405+
* Create one or more Atlas Search indexes for the collection.
406+
*
407+
* Each element in the $indexes array must have a "name" and a "definition" document.
408+
* For example:
409+
*
410+
* $indexes = [
411+
* // Create a search index on all fields
412+
* [ 'name' => 'search_all', 'definition' => [ 'mappings' => [ 'dynamic' => true ] ] ],
413+
* ];
414+
*
415+
* @see https://www.mongodb.com/docs/manual/reference/command/createSearchIndexes/
416+
* @see https://mongodb.com/docs/manual/reference/method/db.collection.createSearchIndex/
417+
* @param array[] $indexes List of search index specifications
418+
* @param array $options Command options
419+
* @return string[] The names of the created search indexes
420+
* @throws UnsupportedException if options are not supported by the selected server
421+
* @throws InvalidArgumentException for parameter/option parsing errors
422+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
413423
*/
414424
public function createSearchIndexes(array $indexes, array $options = []): array
415425
{
416426
$server = select_server($this->manager, $options);
417427

418-
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
419-
$options['writeConcern'] = $this->writeConcern;
420-
}
421-
422-
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes, $options);
428+
$operation = new CreateSearchIndexes($this->databaseName, $this->collectionName, $indexes);
423429

424430
return $operation->execute($server);
425431
}
@@ -602,24 +608,21 @@ public function dropIndexes(array $options = [])
602608
}
603609

604610
/**
605-
* Drop a single search index in the collection.
611+
* Drop a single Atlas Search index in the collection.
606612
*
607-
* @param string|IndexInfo $indexName Index name or model object
608-
* @param array $options Additional options
609-
* @return array|object Command result document
613+
* @param string $name Search index name
614+
* @param array $options Additional options
610615
* @throws UnsupportedException if options are not supported by the selected server
611616
* @throws InvalidArgumentException for parameter/option parsing errors
612617
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
613618
*/
614-
public function dropSearchIndex(string $indexName, array $options = [])
619+
public function dropSearchIndex(string $name, array $options = []): void
615620
{
616-
$indexName = (string) $indexName;
617-
618621
$server = select_server($this->manager, $options);
619622

620-
$operation = new DropSearchIndex($this->databaseName, $this->collectionName, $indexName);
623+
$operation = new DropSearchIndex($this->databaseName, $this->collectionName, $name);
621624

622-
return $operation->execute($server);
625+
$operation->execute($server);
623626
}
624627

625628
/**
@@ -1172,6 +1175,24 @@ public function updateOne($filter, $update, array $options = [])
11721175
return $operation->execute($server);
11731176
}
11741177

1178+
/**
1179+
* Update a single Atlas Search index in the collection.
1180+
*
1181+
* @param string $name Search index name
1182+
* @param array $definition Atlas Search index definition
1183+
* @param array $options Command options
1184+
* @throws UnsupportedException if options are not supported by the selected server
1185+
* @throws InvalidArgumentException for parameter parsing errors
1186+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
1187+
*/
1188+
public function updateSearchIndex(string $name, array $definition, array $options = []): void
1189+
{
1190+
$operation = new UpdateSearchIndex($this->databaseName, $this->collectionName, $name, $definition, $options);
1191+
$server = select_server($this->manager, $options);
1192+
1193+
$operation->execute($server);
1194+
}
1195+
11751196
/**
11761197
* Create a change stream for watching changes to the collection.
11771198
*

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)