Skip to content

Commit fe85f51

Browse files
committed
Complete unit tests
1 parent 45734c6 commit fe85f51

File tree

3 files changed

+206
-115
lines changed

3 files changed

+206
-115
lines changed

src/Scout/ScoutEngine.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use function is_iterable;
4343
use function is_string;
4444
use function iterator_to_array;
45+
use function method_exists;
4546
use function preg_quote;
4647
use function sleep;
4748
use function sprintf;
@@ -188,20 +189,28 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
188189
return $result instanceof CursorInterface ? $result->toArray() : $result;
189190
}
190191

191-
$compound = [];
192-
193-
// Query String
192+
$compound = [
193+
'must' => [
194+
[
195+
'text' => [
196+
'query' => $builder->query,
197+
'path' => ['wildcard' => '*'],
198+
'fuzzy' => ['maxEdits' => 2],
199+
],
200+
],
201+
],
202+
];
194203

195204
foreach ($builder->wheres as $field => $value) {
196-
$compound['filter']['equals'][] = ['path' => $field, 'value' => $value];
205+
$compound['filter'][] = ['equals' => ['path' => $field, 'value' => $value]];
197206
}
198207

199208
foreach ($builder->whereIns as $field => $value) {
200-
$compound['filter']['in'][] = ['path' => $field, 'value' => $value];
209+
$compound['filter'][] = ['in' => ['path' => $field, 'value' => $value]];
201210
}
202211

203212
foreach ($builder->whereNotIns as $field => $value) {
204-
$compound['mustNot']['in'][] = ['path' => $field, 'value' => $value];
213+
$compound['mustNot'][] = ['in' => ['path' => $field, 'value' => $value]];
205214
}
206215

207216
$pipeline = [
@@ -236,27 +245,6 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
236245
return $collection->aggregate($pipeline, $options)->toArray();
237246
}
238247

239-
/**
240-
* Get the filter array for the query.
241-
*
242-
* @return string
243-
*/
244-
protected function filters(Builder $builder): array
245-
{
246-
$filters = $builder->wheres;
247-
248-
// https://www.mongodb.com/docs/atlas/atlas-search/in/
249-
foreach ($builder->whereIns as $field => $values) {
250-
$filters['in'][] = ['path' => $field, 'value' => $values];
251-
}
252-
253-
foreach ($builder->whereNotIns as $field => $values) {
254-
$filters['in'][] = ['path' => $field, 'value' => $values];
255-
}
256-
257-
return $filters;
258-
}
259-
260248
/**
261249
* Pluck and return the primary keys of the given results.
262250
*
@@ -494,6 +482,26 @@ private function performSearchIndexOperation(Closure $closure): void
494482
}
495483
}
496484

485+
private function getMapping(Model $model): array
486+
{
487+
$mapping = self::DEFAULT_DEFINITION;
488+
489+
if (method_exists($model, 'searchableMapping')) {
490+
$mapping = $model->searchableMapping();
491+
}
492+
493+
if ($this->usesSoftDelete($model)) {
494+
// This field is a boolean represented with the integers 0 and 1
495+
$mapping['fields']['__soft_deleted'] = [
496+
'type' => 'number',
497+
'representation' => 'int64',
498+
'indexDoubles' => false,
499+
];
500+
}
501+
502+
return $mapping;
503+
}
504+
497505
/**
498506
* Wait for the callback to return true, use it for asynchronous
499507
* Atlas Search index management operations.

tests/Models/SearchableModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ public function indexableAs(): string
2727
return 'table_indexable';
2828
}
2929

30+
/**
31+
* Overriding the `getScoutKey` method to ensure the custom key is used for indexing
32+
* and searching the model.
33+
*/
3034
public function getScoutKey(): string
3135
{
3236
return $this->getAttribute($this->getScoutKeyName()) ?: 'key_' . $this->getKey();
3337
}
3438

39+
/**
40+
* This method must be overridden when the `getScoutKey` method is also overridden,
41+
* to support model serialization for async indexation jobs.
42+
*/
3543
public function getScoutKeyName(): string
3644
{
3745
return 'scout_key';

0 commit comments

Comments
 (0)