Skip to content

Commit 3e029e9

Browse files
committed
Rename setting and refactor
1 parent c530880 commit 3e029e9

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

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

Lines changed: 14 additions & 15 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;
@@ -17,6 +18,7 @@
1718

1819
use function array_key_first;
1920
use function array_merge;
21+
use function in_array;
2022

2123
/** @phpstan-import-type PipelineExpression from Builder */
2224
final class Aggregation implements IterableResult
@@ -68,7 +70,7 @@ private function prepareIterator(CursorInterface $cursor): Iterator
6870

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

71-
$this->assertSearchIndexExistsWhenAggregationResultsIsEmpty($iterator);
73+
$this->assertSearchIndexExistsForEmptyResult($iterator);
7274

7375
return $iterator;
7476
}
@@ -78,32 +80,29 @@ private function prepareIterator(CursorInterface $cursor): Iterator
7880
* this assertion can be removed.
7981
*
8082
* @see https://jira.mongodb.org/browse/SERVER-110974
83+
* @see Configuration::setAssertSearchIndexExistsForEmptyResult()
8184
*
82-
* @param Iterator<object> $iterator
85+
* @param CachingIterator<mixed>|UnrewindableIterator<mixed> $iterator
8386
*/
84-
private function assertSearchIndexExistsWhenAggregationResultsIsEmpty(Iterator $iterator): void
87+
private function assertSearchIndexExistsForEmptyResult(CachingIterator|UnrewindableIterator $iterator): void
8588
{
86-
if ($iterator->current() !== false) {
89+
// The iterator is always rewinded
90+
if ($iterator->current()) {
8791
return; // Results not empty
8892
}
8993

90-
if (! $this->dm->getConfiguration()->assertSearchIndexExistsWhenAggregationResultsIsEmpty()) {
94+
if (! $this->dm->getConfiguration()->assertSearchIndexExistsForEmptyResult()) {
9195
return; // Feature disabled
9296
}
9397

9498
// 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
99+
$stage = $this->pipeline[0] ?? null;
100+
if (! $stage || ! in_array(array_key_first($stage), ['$search', '$searchMeta', '$vectorSearch'], true)) {
101+
return; // Not a search aggregation
104102
}
105103

106-
$index = $this->collection->listSearchIndexes(['filter' => ['name' => $indexName]])->current();
104+
$indexName = ((object) $stage[array_key_first($stage)])->index ?? 'default';
105+
$index = $this->collection->listSearchIndexes(['filter' => ['name' => $indexName]])->current();
107106
if ($index) {
108107
return; // Index exists
109108
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()

0 commit comments

Comments
 (0)