2121use MongoDB \Exception \RuntimeException as MongoDBRuntimeException ;
2222use MongoDB \Laravel \Connection ;
2323use Override ;
24+ use stdClass ;
2425use Traversable ;
2526use TypeError ;
2627
@@ -212,6 +213,13 @@ private function performSearch(Builder $builder, ?int $offset = null): array
212213 return $ cursor ->toArray ();
213214 }
214215
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.
215223 $ compound = [
216224 'should ' => [
217225 [
@@ -236,7 +244,6 @@ private function performSearch(Builder $builder, ?int $offset = null): array
236244 // "filter" specifies conditions on exact values to match
237245 // "mustNot" specifies conditions on exact values that must not match
238246 // They don't contribute to the relevance score
239- // https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
240247 foreach ($ builder ->wheres as $ field => $ value ) {
241248 if ($ field === '__soft_deleted ' ) {
242249 $ value = (bool ) $ value ;
@@ -378,8 +385,9 @@ private function performMap(Builder $builder, array $results, Model $model, bool
378385 * This is an estimate if the count is larger than 1000.
379386 *
380387 * @see Engine::getTotalCount()
388+ * @see https://www.mongodb.com/docs/atlas/atlas-search/counting/
381389 *
382- * @param mixed $results
390+ * @param stdClass[] $results
383391 */
384392 #[Override]
385393 public function getTotalCount ($ results ): int
@@ -388,6 +396,8 @@ public function getTotalCount($results): int
388396 return 0 ;
389397 }
390398
399+ // __count field is added by the aggregation pipeline in performSearch()
400+ // using the count.lowerBound in the $search stage
391401 return $ results [0 ]->__count ;
392402 }
393403
0 commit comments