Skip to content

Commit 3b1d68e

Browse files
committed
Convert __soft_deleted to bool, and return root documents as array (typeMap)
1 parent caaf925 commit 3b1d68e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/Scout/ScoutEngine.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
use MongoDB\Database;
2121
use MongoDB\Driver\Cursor;
2222
use MongoDB\Driver\CursorInterface;
23-
use MongoDB\Exception\Exception;
24-
use MongoDB\Exception\RuntimeException;
23+
use MongoDB\Exception\RuntimeException as MongoDBRuntimeException;
2524
use MongoDB\Laravel\Connection;
2625
use Traversable;
2726
use TypeError;
@@ -79,15 +78,14 @@ public function __construct(
7978
*
8079
* @param EloquentCollection $models
8180
*
82-
* @throws Exception
81+
* @throws MongoDBRuntimeException
8382
*/
8483
public function update($models)
8584
{
8685
if ($models->isEmpty()) {
8786
return;
8887
}
8988

90-
$collection = $this->getIndexableCollection($models);
9189

9290
if ($this->softDelete && $this->usesSoftDelete($models)) {
9391
$models->each->pushSoftDeleteMetadata();
@@ -101,12 +99,17 @@ public function update($models)
10199
if ($searchableData) {
102100
unset($searchableData['_id']);
103101

102+
$searchableData = array_merge($searchableData, $model->scoutMetadata());
103+
if (isset($searchableData['__soft_deleted'])) {
104+
$searchableData['__soft_deleted'] = (bool) $searchableData['__soft_deleted'];
105+
}
106+
104107
$bulk[] = [
105108
'updateOne' => [
106109
['_id' => $model->getScoutKey()],
107110
[
108111
'$setOnInsert' => ['_id' => $model->getScoutKey()],
109-
'$set' => array_merge($searchableData, $model->scoutMetadata()),
112+
'$set' => $searchableData,
110113
],
111114
['upsert' => true],
112115
],
@@ -115,7 +118,7 @@ public function update($models)
115118
}
116119

117120
if ($bulk) {
118-
$collection->bulkWrite($bulk);
121+
$this->getIndexableCollection($models)->bulkWrite($bulk);
119122
}
120123
}
121124

@@ -180,9 +183,9 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
180183
$builder->query,
181184
$offset,
182185
);
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))));
184187

185-
return $result instanceof CursorInterface ? $result->toArray() : $result;
188+
return $result->toArray();
186189
}
187190

188191
$compound = [
@@ -211,6 +214,10 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
211214
// They don't contribute to the relevance score
212215
// https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
213216
foreach ($builder->wheres as $field => $value) {
217+
if ($field === '__soft_deleted') {
218+
$value = (bool) $value;
219+
}
220+
214221
$compound['filter'][] = ['equals' => ['path' => $field, 'value' => $value]];
215222
}
216223

@@ -253,8 +260,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
253260
}
254261

255262
$options = [
256-
'allowDiskUse' => true,
257-
'typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array'],
263+
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
258264
];
259265

260266
return $collection->aggregate($pipeline, $options)->toArray();
@@ -322,7 +328,7 @@ public function getTotalCount($results): int
322328
return 0;
323329
}
324330

325-
return $results[0]->__count;
331+
return $results[0]['__count'];
326332
}
327333

328334
/**
@@ -513,9 +519,7 @@ private function getMapping(Model $model): array
513519
// This field is a boolean represented with the integers 0 and 1
514520
// https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
515521
$mapping['fields']['__soft_deleted'] ??= [
516-
'type' => 'number',
517-
'representation' => 'int64',
518-
'indexDoubles' => false,
522+
'type' => 'boolean',
519523
];
520524
}
521525

@@ -537,6 +541,6 @@ private function wait(Closure $callback): void
537541
sleep(1);
538542
}
539543

540-
throw new RuntimeException('Atlas search index operation time out');
544+
throw new MongoDBRuntimeException('Atlas search index operation time out');
541545
}
542546
}

tests/Scout/ScoutEngineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/** Unit tests that do not require an Atlas Search cluster */
3434
class ScoutEngineTest extends TestCase
3535
{
36-
private const EXPECTED_SEARCH_OPTIONS = ['allowDiskUse' => true, 'typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array']];
36+
private const EXPECTED_SEARCH_OPTIONS = ['typeMap' => ['root' => 'object', 'document' => 'array', 'array' => 'array']];
3737

3838
/** @param callable(): Builder $builder */
3939
#[DataProvider('provideSearchPipelines')]

0 commit comments

Comments
 (0)