Skip to content

Commit ff1a0af

Browse files
committed
Rename setting and refactor
1 parent c530880 commit ff1a0af

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Aggregation;
66

7+
use Doctrine\ODM\MongoDB\Configuration;
78
use Doctrine\ODM\MongoDB\DocumentManager;
89
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
910
use Doctrine\ODM\MongoDB\Iterator\HydratingIterator;
@@ -15,8 +16,10 @@
1516
use MongoDB\Collection;
1617
use MongoDB\Driver\CursorInterface;
1718

18-
use function array_key_first;
1919
use function array_merge;
20+
use function current;
21+
use function in_array;
22+
use function key;
2023

2124
/** @phpstan-import-type PipelineExpression from Builder */
2225
final class Aggregation implements IterableResult
@@ -68,7 +71,7 @@ private function prepareIterator(CursorInterface $cursor): Iterator
6871

6972
$iterator = $this->rewindable ? new CachingIterator($cursor) : new UnrewindableIterator($cursor);
7073

71-
$this->assertSearchIndexExistsWhenAggregationResultsIsEmpty($iterator);
74+
$this->assertSearchIndexExistsForEmptyResult($iterator);
7275

7376
return $iterator;
7477
}
@@ -78,32 +81,30 @@ private function prepareIterator(CursorInterface $cursor): Iterator
7881
* this assertion can be removed.
7982
*
8083
* @see https://jira.mongodb.org/browse/SERVER-110974
84+
* @see Configuration::setAssertSearchIndexExistsForEmptyResult()
8185
*
82-
* @param Iterator<object> $iterator
86+
* @param CachingIterator<mixed>|UnrewindableIterator<mixed> $iterator
8387
*/
84-
private function assertSearchIndexExistsWhenAggregationResultsIsEmpty(Iterator $iterator): void
88+
private function assertSearchIndexExistsForEmptyResult(CachingIterator|UnrewindableIterator $iterator): void
8589
{
86-
if ($iterator->current() !== false) {
90+
// The iterator is always rewinded
91+
if ($iterator->current()) {
8792
return; // Results not empty
8893
}
8994

90-
if (! $this->dm->getConfiguration()->assertSearchIndexExistsWhenAggregationResultsIsEmpty()) {
95+
if (! $this->dm->getConfiguration()->assertSearchIndexExistsForEmptyResult()) {
9196
return; // Feature disabled
9297
}
9398

9499
// Search stages must be the first stage in the pipeline
95-
$indexName = match (array_key_first($this->pipeline[0])) {
96-
'$search' => $this->pipeline[0]['$search']->index ?? null,
97-
'$searchMeta' => $this->pipeline[0]['$searchMeta']->index ?? null,
98-
'$vectorSearch' => $this->pipeline[0]['$vectorSearch']->index ?? null,
99-
default => null,
100-
};
101-
102-
if ($indexName === null) {
103-
return; // Not a search aggregation or index not specified
100+
$stage = $this->pipeline[0] ?? null;
101+
if (! $stage || ! in_array(key($stage), ['$search', '$searchMeta', '$vectorSearch'], true)) {
102+
return; // Not a search aggregation
104103
}
105104

106-
$index = $this->collection->listSearchIndexes(['filter' => ['name' => $indexName]])->current();
105+
// @phpcs:ignore SlevomatCodingStandard.PHP.UselessParentheses
106+
$indexName = ((object) current($stage))->index ?? 'default';
107+
$index = $this->collection->listSearchIndexes(['filter' => ['name' => $indexName]])->current();
107108
if ($index) {
108109
return; // Index exists
109110
}

lib/Doctrine/ODM/MongoDB/Configuration.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -812,18 +812,19 @@ private function getAutoEncryptionOptions(): array
812812
/**
813813
* Pipelines using a search index that does not exist or is not queryable
814814
* will return zero documents. By enabling this feature, an additional query
815-
* is made when the pipeline doesn't return any results to check if the
816-
* search index exists and is queryable. If the index does not exist or is
817-
* not queryable, an exception is thrown. This feature is enabled by default.
815+
* is performed when the pipeline doesn't return any results to check if the
816+
* search index exists. If the index does not exist, an exception is thrown.
817+
* This feature is enabled by default.
818+
* This applies to $search, $searchMeta and $vectorSearch pipelines.
818819
*/
819-
public function setAssertSearchIndexExistsWhenAggregationResultsIsEmpty(bool $enabled): void
820+
public function setAssertSearchIndexExistsForEmptyResult(bool $enabled): void
820821
{
821-
$this->attributes['assertSearchIndexExistsWhenAggregationResultsIsEmpty'] = $enabled;
822+
$this->attributes['assertSearchIndexExistsForEmptyResult'] = $enabled;
822823
}
823824

824-
public function assertSearchIndexExistsWhenAggregationResultsIsEmpty(): bool
825+
public function assertSearchIndexExistsForEmptyResult(): bool
825826
{
826-
return $this->attributes['assertSearchIndexExistsWhenAggregationResultsIsEmpty'] ?? true;
827+
return $this->attributes['assertSearchIndexExistsForEmptyResult'] ?? true;
827828
}
828829
}
829830

tests/Doctrine/ODM/MongoDB/Tests/Functional/AtlasSearchTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\ODM\MongoDB\Tests\Functional;
66

7+
use Doctrine\ODM\MongoDB\Aggregation\Stage;
78
use Doctrine\ODM\MongoDB\SchemaException;
89
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
910
use Documents\CmsArticle;
@@ -55,7 +56,6 @@ public function testAtlasSearch(): void
5556
$schemaManager->waitForSearchIndexes([CmsArticle::class, CmsUser::class]);
5657

5758
$results = $this->dm->createAggregationBuilder(CmsArticle::class)
58-
->rewindable(false)
5959
->search()
6060
->index('search_articles')
6161
->autocomplete()
@@ -137,7 +137,7 @@ public function testIndexNotCreated(): void
137137

138138
public function testIndexNotCreatedWithoutException(): void
139139
{
140-
$this->dm->getConfiguration()->setAssertSearchIndexExistsWhenAggregationResultsIsEmpty(false);
140+
$this->dm->getConfiguration()->setAssertSearchIndexExistsForEmptyResult(false);
141141

142142
$results = $this->dm->createAggregationBuilder(CmsArticle::class)
143143
->search()
@@ -149,4 +149,20 @@ public function testIndexNotCreatedWithoutException(): void
149149

150150
$this->assertCount(0, $results->toArray());
151151
}
152+
153+
public function testIndexNotCreatedWithCustomStage(): void
154+
{
155+
$aggregation = ($builder = $this->dm->createAggregationBuilder(CmsArticle::class))
156+
->addStage(new class ($builder) extends Stage {
157+
public function getExpression(): array
158+
{
159+
return ['$search' => ['text' => ['query' => 'Atlas Search', 'path' => 'text']]];
160+
}
161+
})->getAggregation();
162+
163+
$this->expectException(SchemaException::class);
164+
$this->expectExceptionMessageMatches('#^The search index "default" of the collection "[^."]+\.CmsArticle" is not found\.$#');
165+
166+
$aggregation->execute();
167+
}
152168
}

0 commit comments

Comments
 (0)