20
20
use MongoDB \Database ;
21
21
use MongoDB \Driver \Cursor ;
22
22
use MongoDB \Driver \CursorInterface ;
23
- use MongoDB \Exception \Exception ;
24
- use MongoDB \Exception \RuntimeException ;
23
+ use MongoDB \Exception \RuntimeException as MongoDBRuntimeException ;
25
24
use MongoDB \Laravel \Connection ;
26
25
use Traversable ;
27
26
use TypeError ;
@@ -79,15 +78,14 @@ public function __construct(
79
78
*
80
79
* @param EloquentCollection $models
81
80
*
82
- * @throws Exception
81
+ * @throws MongoDBRuntimeException
83
82
*/
84
83
public function update ($ models )
85
84
{
86
85
if ($ models ->isEmpty ()) {
87
86
return ;
88
87
}
89
88
90
- $ collection = $ this ->getIndexableCollection ($ models );
91
89
92
90
if ($ this ->softDelete && $ this ->usesSoftDelete ($ models )) {
93
91
$ models ->each ->pushSoftDeleteMetadata ();
@@ -101,12 +99,17 @@ public function update($models)
101
99
if ($ searchableData ) {
102
100
unset($ searchableData ['_id ' ]);
103
101
102
+ $ searchableData = array_merge ($ searchableData , $ model ->scoutMetadata ());
103
+ if (isset ($ searchableData ['__soft_deleted ' ])) {
104
+ $ searchableData ['__soft_deleted ' ] = (bool ) $ searchableData ['__soft_deleted ' ];
105
+ }
106
+
104
107
$ bulk [] = [
105
108
'updateOne ' => [
106
109
['_id ' => $ model ->getScoutKey ()],
107
110
[
108
111
'$setOnInsert ' => ['_id ' => $ model ->getScoutKey ()],
109
- '$set ' => array_merge ( $ searchableData, $ model -> scoutMetadata ()) ,
112
+ '$set ' => $ searchableData ,
110
113
],
111
114
['upsert ' => true ],
112
115
],
@@ -115,7 +118,7 @@ public function update($models)
115
118
}
116
119
117
120
if ($ bulk ) {
118
- $ collection ->bulkWrite ($ bulk );
121
+ $ this -> getIndexableCollection ( $ models ) ->bulkWrite ($ bulk );
119
122
}
120
123
}
121
124
@@ -180,9 +183,9 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
180
183
$ builder ->query ,
181
184
$ offset ,
182
185
);
183
- assert ($ result instanceof CursorInterface || is_array ( $ result ) , new LogicException (sprintf ('The search builder closure must return an array or a MongoDB cursor, %s returned ' , get_debug_type ($ result ))));
186
+ assert ($ result instanceof CursorInterface, new LogicException (sprintf ('The search builder closure must return a MongoDB cursor, %s returned ' , get_debug_type ($ result ))));
184
187
185
- return $ result instanceof CursorInterface ? $ result ->toArray () : $ result ;
188
+ return $ result ->toArray ();
186
189
}
187
190
188
191
$ compound = [
@@ -211,6 +214,10 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
211
214
// They don't contribute to the relevance score
212
215
// https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
213
216
foreach ($ builder ->wheres as $ field => $ value ) {
217
+ if ($ field === '__soft_deleted ' ) {
218
+ $ value = (bool ) $ value ;
219
+ }
220
+
214
221
$ compound ['filter ' ][] = ['equals ' => ['path ' => $ field , 'value ' => $ value ]];
215
222
}
216
223
@@ -253,8 +260,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
253
260
}
254
261
255
262
$ options = [
256
- 'allowDiskUse ' => true ,
257
- 'typeMap ' => ['root ' => 'object ' , 'document ' => 'array ' , 'array ' => 'array ' ],
263
+ 'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' , 'array ' => 'array ' ],
258
264
];
259
265
260
266
return $ collection ->aggregate ($ pipeline , $ options )->toArray ();
@@ -322,7 +328,7 @@ public function getTotalCount($results): int
322
328
return 0 ;
323
329
}
324
330
325
- return $ results [0 ]-> __count ;
331
+ return $ results [0 ][ ' __count ' ] ;
326
332
}
327
333
328
334
/**
@@ -513,9 +519,7 @@ private function getMapping(Model $model): array
513
519
// This field is a boolean represented with the integers 0 and 1
514
520
// https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
515
521
$ mapping ['fields ' ]['__soft_deleted ' ] ??= [
516
- 'type ' => 'number ' ,
517
- 'representation ' => 'int64 ' ,
518
- 'indexDoubles ' => false ,
522
+ 'type ' => 'boolean ' ,
519
523
];
520
524
}
521
525
@@ -537,6 +541,6 @@ private function wait(Closure $callback): void
537
541
sleep (1 );
538
542
}
539
543
540
- throw new RuntimeException ('Atlas search index operation time out ' );
544
+ throw new MongoDBRuntimeException ('Atlas search index operation time out ' );
541
545
}
542
546
}
0 commit comments