21
21
use MongoDB \Exception \RuntimeException as MongoDBRuntimeException ;
22
22
use MongoDB \Laravel \Connection ;
23
23
use Override ;
24
+ use stdClass ;
24
25
use Traversable ;
25
26
use TypeError ;
26
27
@@ -212,6 +213,13 @@ private function performSearch(Builder $builder, ?int $offset = null): array
212
213
return $ cursor ->toArray ();
213
214
}
214
215
216
+ // Using compound to combine search operators
217
+ // https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
218
+ // "should" specifies conditions that contribute to the relevance score
219
+ // at least one of them must match,
220
+ // - "text" search for the text including fuzzy matching
221
+ // - "wildcard" allows special characters like * and ?, similar to LIKE in SQL
222
+ // These are the only search operators to accept wildcard path.
215
223
$ compound = [
216
224
'should ' => [
217
225
[
@@ -236,7 +244,6 @@ private function performSearch(Builder $builder, ?int $offset = null): array
236
244
// "filter" specifies conditions on exact values to match
237
245
// "mustNot" specifies conditions on exact values that must not match
238
246
// They don't contribute to the relevance score
239
- // https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
240
247
foreach ($ builder ->wheres as $ field => $ value ) {
241
248
if ($ field === '__soft_deleted ' ) {
242
249
$ value = (bool ) $ value ;
@@ -378,8 +385,9 @@ private function performMap(Builder $builder, array $results, Model $model, bool
378
385
* This is an estimate if the count is larger than 1000.
379
386
*
380
387
* @see Engine::getTotalCount()
388
+ * @see https://www.mongodb.com/docs/atlas/atlas-search/counting/
381
389
*
382
- * @param mixed $results
390
+ * @param stdClass[] $results
383
391
*/
384
392
#[Override]
385
393
public function getTotalCount ($ results ): int
@@ -388,6 +396,8 @@ public function getTotalCount($results): int
388
396
return 0 ;
389
397
}
390
398
399
+ // __count field is added by the aggregation pipeline in performSearch()
400
+ // using the count.lowerBound in the $search stage
391
401
return $ results [0 ]->__count ;
392
402
}
393
403
0 commit comments