4
4
5
5
namespace Doctrine \ODM \MongoDB \Aggregation ;
6
6
7
+ use Doctrine \ODM \MongoDB \Configuration ;
7
8
use Doctrine \ODM \MongoDB \DocumentManager ;
8
9
use Doctrine \ODM \MongoDB \Iterator \CachingIterator ;
9
10
use Doctrine \ODM \MongoDB \Iterator \HydratingIterator ;
17
18
18
19
use function array_key_first ;
19
20
use function array_merge ;
21
+ use function in_array ;
20
22
21
23
/** @phpstan-import-type PipelineExpression from Builder */
22
24
final class Aggregation implements IterableResult
@@ -68,7 +70,7 @@ private function prepareIterator(CursorInterface $cursor): Iterator
68
70
69
71
$ iterator = $ this ->rewindable ? new CachingIterator ($ cursor ) : new UnrewindableIterator ($ cursor );
70
72
71
- $ this ->assertSearchIndexExistsWhenAggregationResultsIsEmpty ($ iterator );
73
+ $ this ->assertSearchIndexExistsForEmptyResult ($ iterator );
72
74
73
75
return $ iterator ;
74
76
}
@@ -78,32 +80,29 @@ private function prepareIterator(CursorInterface $cursor): Iterator
78
80
* this assertion can be removed.
79
81
*
80
82
* @see https://jira.mongodb.org/browse/SERVER-110974
83
+ * @see Configuration::setAssertSearchIndexExistsForEmptyResult()
81
84
*
82
- * @param Iterator<object > $iterator
85
+ * @param CachingIterator<mixed>|UnrewindableIterator<mixed > $iterator
83
86
*/
84
- private function assertSearchIndexExistsWhenAggregationResultsIsEmpty ( Iterator $ iterator ): void
87
+ private function assertSearchIndexExistsForEmptyResult ( CachingIterator | UnrewindableIterator $ iterator ): void
85
88
{
86
- if ($ iterator ->current () !== false ) {
89
+ // The iterator is always rewinded
90
+ if ($ iterator ->current ()) {
87
91
return ; // Results not empty
88
92
}
89
93
90
- if (! $ this ->dm ->getConfiguration ()->assertSearchIndexExistsWhenAggregationResultsIsEmpty ()) {
94
+ if (! $ this ->dm ->getConfiguration ()->assertSearchIndexExistsForEmptyResult ()) {
91
95
return ; // Feature disabled
92
96
}
93
97
94
98
// 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
104
102
}
105
103
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 ();
107
106
if ($ index ) {
108
107
return ; // Index exists
109
108
}
0 commit comments