54
54
*/
55
55
final class ScoutEngine extends Engine
56
56
{
57
- /**
58
- * Name of the Atlas Search index.
59
- *
60
- * @todo make this configurable
61
- */
57
+ /** Name of the Atlas Search index. */
62
58
private const INDEX_NAME = 'scout ' ;
63
59
64
60
// Atlas Search index management operations are asynchronous.
@@ -201,6 +197,10 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
201
197
],
202
198
];
203
199
200
+ // "filter" specifies conditions on exact values to match
201
+ // "mustNot" specifies conditions on exact values that must not match
202
+ // They don't contribute to the relevance score
203
+ // https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
204
204
foreach ($ builder ->wheres as $ field => $ value ) {
205
205
$ compound ['filter ' ][] = ['equals ' => ['path ' => $ field , 'value ' => $ value ]];
206
206
}
@@ -213,6 +213,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
213
213
$ compound ['mustNot ' ][] = ['in ' => ['path ' => $ field , 'value ' => $ value ]];
214
214
}
215
215
216
+ // Sort by field value only if specified
216
217
$ sort = [];
217
218
foreach ($ builder ->orders as $ order ) {
218
219
$ sort [$ order ['column ' ]] = $ order ['direction ' ] === 'asc ' ? 1 : -1 ;
@@ -224,7 +225,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
224
225
'index ' => self ::INDEX_NAME ,
225
226
'compound ' => $ compound ,
226
227
'count ' => ['type ' => 'lowerBound ' ],
227
- ...($ builder -> orders ? ['sort ' => $ sort ] : []),
228
+ ...($ sort ? ['sort ' => $ sort ] : []),
228
229
],
229
230
],
230
231
[
@@ -482,7 +483,7 @@ private function performSearchIndexOperation(Closure $closure): void
482
483
} catch (ServerException $ exception ) {
483
484
// @todo add error codes
484
485
if (in_array ($ exception ->getCode (), [])) {
485
- throw new \RuntimeException ('Failed to perform search index operation. MongoDB Atlas cluster M10+ is required ' , 0 , $ exception );
486
+ throw new \RuntimeException ('Failed to perform search index operation. A MongoDB Atlas Cluster is required. ' , 0 , $ exception );
486
487
}
487
488
}
488
489
}
@@ -497,6 +498,7 @@ private function getMapping(Model $model): array
497
498
498
499
if ($ this ->usesSoftDelete ($ model )) {
499
500
// This field is a boolean represented with the integers 0 and 1
501
+ // https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
500
502
$ mapping ['fields ' ]['__soft_deleted ' ] ??= [
501
503
'type ' => 'number ' ,
502
504
'representation ' => 'int64 ' ,
0 commit comments