Skip to content

Commit

Permalink
Complete unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 18, 2024
1 parent 45734c6 commit 4647cf0
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 116 deletions.
69 changes: 41 additions & 28 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use function is_iterable;
use function is_string;
use function iterator_to_array;
use function method_exists;
use function preg_quote;
use function sleep;
use function sprintf;
Expand Down Expand Up @@ -188,20 +189,33 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
return $result instanceof CursorInterface ? $result->toArray() : $result;
}

$compound = [];

// Query String
$compound = [
'must' => [
[
'text' => [
'query' => $builder->query,
'path' => ['wildcard' => '*'],
'fuzzy' => ['maxEdits' => 2],
],
],
],
];

foreach ($builder->wheres as $field => $value) {
$compound['filter']['equals'][] = ['path' => $field, 'value' => $value];
$compound['filter'][] = ['equals' => ['path' => $field, 'value' => $value]];
}

foreach ($builder->whereIns as $field => $value) {
$compound['filter']['in'][] = ['path' => $field, 'value' => $value];
$compound['filter'][] = ['in' => ['path' => $field, 'value' => $value]];
}

foreach ($builder->whereNotIns as $field => $value) {
$compound['mustNot']['in'][] = ['path' => $field, 'value' => $value];
$compound['mustNot'][] = ['in' => ['path' => $field, 'value' => $value]];
}

$sort = [];
foreach ($builder->orders as $order) {
$sort[$order['column']] = $order['direction'] === 'asc' ? 1 : -1;
}

$pipeline = [
Expand All @@ -210,7 +224,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
'index' => self::INDEX_NAME,
'compound' => $compound,
'count' => ['type' => 'lowerBound'],
'sort' => array_merge(...array_map(fn ($order) => [$order['column'] => $order['direction'] === 'asc' ? 1 : -1], $builder->orders)),
...($builder->orders ? ['sort' => $sort] : []),
],
],
[
Expand All @@ -236,27 +250,6 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
return $collection->aggregate($pipeline, $options)->toArray();
}

/**
* Get the filter array for the query.
*
* @return string
*/
protected function filters(Builder $builder): array
{
$filters = $builder->wheres;

// https://www.mongodb.com/docs/atlas/atlas-search/in/
foreach ($builder->whereIns as $field => $values) {
$filters['in'][] = ['path' => $field, 'value' => $values];
}

foreach ($builder->whereNotIns as $field => $values) {
$filters['in'][] = ['path' => $field, 'value' => $values];
}

return $filters;
}

/**
* Pluck and return the primary keys of the given results.
*
Expand Down Expand Up @@ -494,6 +487,26 @@ private function performSearchIndexOperation(Closure $closure): void
}
}

private function getMapping(Model $model): array
{
$mapping = self::DEFAULT_DEFINITION;

if (method_exists($model, 'searchableMapping')) {
$mapping = $model->searchableMapping();
}

if ($this->usesSoftDelete($model)) {
// This field is a boolean represented with the integers 0 and 1
$mapping['fields']['__soft_deleted'] ??= [
'type' => 'number',
'representation' => 'int64',
'indexDoubles' => false,
];
}

return $mapping;
}

/**
* Wait for the callback to return true, use it for asynchronous
* Atlas Search index management operations.
Expand Down
8 changes: 8 additions & 0 deletions tests/Models/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ public function indexableAs(): string
return 'table_indexable';
}

/**
* Overriding the `getScoutKey` method to ensure the custom key is used for indexing
* and searching the model.
*/
public function getScoutKey(): string
{
return $this->getAttribute($this->getScoutKeyName()) ?: 'key_' . $this->getKey();
}

/**
* This method must be overridden when the `getScoutKey` method is also overridden,
* to support model serialization for async indexation jobs.
*/
public function getScoutKeyName(): string
{
return 'scout_key';
Expand Down
Loading

0 comments on commit 4647cf0

Please sign in to comment.