Skip to content

Commit

Permalink
Clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 21, 2024
1 parent e02da52 commit c75317a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@
*/
final class ScoutEngine extends Engine
{
/**
* Name of the Atlas Search index.
*
* @todo make this configurable
*/
/** Name of the Atlas Search index. */
private const INDEX_NAME = 'scout';

// Atlas Search index management operations are asynchronous.
Expand Down Expand Up @@ -201,6 +197,10 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
],
];

// "filter" specifies conditions on exact values to match
// "mustNot" specifies conditions on exact values that must not match
// They don't contribute to the relevance score
// https://www.mongodb.com/docs/atlas/atlas-search/compound/#options
foreach ($builder->wheres as $field => $value) {
$compound['filter'][] = ['equals' => ['path' => $field, 'value' => $value]];
}
Expand All @@ -213,6 +213,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
$compound['mustNot'][] = ['in' => ['path' => $field, 'value' => $value]];
}

// Sort by field value only if specified
$sort = [];
foreach ($builder->orders as $order) {
$sort[$order['column']] = $order['direction'] === 'asc' ? 1 : -1;
Expand All @@ -224,7 +225,7 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
'index' => self::INDEX_NAME,
'compound' => $compound,
'count' => ['type' => 'lowerBound'],
...($builder->orders ? ['sort' => $sort] : []),
...($sort ? ['sort' => $sort] : []),
],
],
[
Expand Down Expand Up @@ -482,7 +483,7 @@ private function performSearchIndexOperation(Closure $closure): void
} catch (ServerException $exception) {
// @todo add error codes
if (in_array($exception->getCode(), [])) {
throw new \RuntimeException('Failed to perform search index operation. MongoDB Atlas cluster M10+ is required', 0, $exception);
throw new \RuntimeException('Failed to perform search index operation. A MongoDB Atlas Cluster is required.', 0, $exception);
}
}
}
Expand All @@ -497,6 +498,7 @@ private function getMapping(Model $model): array

if ($this->usesSoftDelete($model)) {
// This field is a boolean represented with the integers 0 and 1
// https://www.mongodb.com/docs/atlas/atlas-search/field-types/number-type/#configure-fts-field-type-field-properties
$mapping['fields']['__soft_deleted'] ??= [
'type' => 'number',
'representation' => 'int64',
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/SchemaVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\HasSchemaVersion;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\Model;

class SchemaVersion extends Eloquent
class SchemaVersion extends Model
{
use HasSchemaVersion;

Expand Down
4 changes: 2 additions & 2 deletions tests/Models/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace MongoDB\Laravel\Tests\Models;

use Laravel\Scout\Searchable;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Eloquent\SoftDeletes;

class SearchableModel extends Eloquent
class SearchableModel extends Model
{
use Searchable;
use SoftDeletes;
Expand Down
5 changes: 2 additions & 3 deletions tests/Scout/ScoutEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Closure;
use DateTimeImmutable;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as IlluminateCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Jobs\RemoveFromSearch;
use Laravel\Scout\Tests\Unit\AlgoliaEngineTest;
use Mockery as m;
use MongoDB\BSON\Document;
use MongoDB\BSON\Regex;
Expand Down Expand Up @@ -390,7 +390,7 @@ public function testMapIds(array $results): void

$ids = $engine->mapIds($results);

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $ids);
$this->assertInstanceOf(IlluminateCollection::class, $ids);
$this->assertEquals(['key_1', 'key_2'], $ids->all());
}

Expand Down Expand Up @@ -509,7 +509,6 @@ public function testDelete(): void
]));
}

/** @see AlgoliaEngineTest::test_delete_with_removeable_scout_collection_using_custom_search_key */
public function testDeleteWithRemoveableScoutCollection(): void
{
$job = new RemoveFromSearch(EloquentCollection::make([
Expand Down
8 changes: 2 additions & 6 deletions tests/Scout/ScoutIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ class ScoutIntegrationTest extends TestCase
{
protected static string $userModel = SqlUser::class;

protected function defineEnvironment($app)
{
$app['config']->set('scout.driver', 'mongodb');
$app['config']->set('scout.prefix', 'scout_');
}

protected function defineDatabaseMigrations(): void
{
SqlUser::executeSchema();
Expand Down Expand Up @@ -59,6 +53,8 @@ protected function defineDatabaseMigrations(): void
->state(new Sequence(...$collect->all()))
->create();

// Recreate the indexes using the artisan commands
// Ensure they return a success exit code (0)
self::assertSame(0, artisan($this, 'scout:delete-index', ['name' => SqlUser::class]));
self::assertSame(0, artisan($this, 'scout:index', ['name' => SqlUser::class]));
self::assertSame(0, artisan($this, 'scout:import', ['model' => SqlUser::class]));
Expand Down

0 comments on commit c75317a

Please sign in to comment.