Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/10.x' into feature-test
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Dec 5, 2023
2 parents 141ea87 + 13fd42b commit 66934d6
Show file tree
Hide file tree
Showing 19 changed files with 194 additions and 61 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ['8.0', 8.1, 8.2]
php: ['8.0', 8.1, 8.2, 8.3]
laravel: [9, 10]
exclude:
- php: '8.0'
laravel: 9
- php: '8.0'
laravel: 10
- php: 8.3
laravel: 9

name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Release Notes

## [Unreleased](https://github.com/laravel/scout/compare/v10.3.0...10.x)
## [Unreleased](https://github.com/laravel/scout/compare/v10.6.0...10.x)

## [v10.6.0](https://github.com/laravel/scout/compare/v10.5.1...v10.6.0) - 2023-11-28

* Add search engine meta data to results by [@tobz-nz](https://github.com/tobz-nz) in https://github.com/laravel/scout/pull/780

## [v10.5.1](https://github.com/laravel/scout/compare/v10.5.0...v10.5.1) - 2023-10-31

- Call makeSearchableUsing before searching on CollectionEngine by [@Magnesium38](https://github.com/Magnesium38) in https://github.com/laravel/scout/pull/777

## [v10.5.0](https://github.com/laravel/scout/compare/v10.4.0...v10.5.0) - 2023-10-10

- Adds latest and oldest sorting options by [@peterfox](https://github.com/peterfox) in https://github.com/laravel/scout/pull/770

## [v10.4.0](https://github.com/laravel/scout/compare/v10.3.0...v10.4.0) - 2023-09-26

- Allow configuration of Algolia batch size by [@samlev](https://github.com/samlev) in https://github.com/laravel/scout/pull/768
**Full Changelog**: https://github.com/laravel/scout/compare/v10.3.0...v10.4.0

## [v10.3.0](https://github.com/laravel/scout/compare/v10.2.4...v10.3.0) - 2023-09-05

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"algolia/algoliasearch-client-php": "^3.2",
"meilisearch/meilisearch-php": "^1.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^7.0|^8.0",
"orchestra/testbench": "^7.31|^8.11",
"php-http/guzzle7-adapter": "^1.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3"
Expand Down
22 changes: 22 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@ public function orderBy($column, $direction = 'asc')
return $this;
}

/**
* Add an "order by" clause for a timestamp to the query.
*
* @param string $column
* @return $this
*/
public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}

/**
* Add an "order by" clause for a timestamp to the query.
*
* @param string $column
* @return $this
*/
public function oldest($column = 'created_at')
{
return $this->orderBy($column, 'asc');
}

/**
* Set extra options for the search query.
*
Expand Down
4 changes: 4 additions & 0 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function createAlgoliaDriver()
$config->setWriteTimeout($writeTimeout);
}

if (is_int($batchSize = config('scout.algolia.batch_size'))) {
$config->setBatchSize($batchSize);
}

return new AlgoliaEngine(Algolia::createWithConfig($config), config('scout.soft_delete'));
}

Expand Down
20 changes: 20 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ public function map(Builder $builder, $results, $model)
$builder, $objectIds
)->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand All @@ -236,6 +246,16 @@ public function lazyMap(Builder $builder, $results, $model)
$builder, $objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand Down
2 changes: 1 addition & 1 deletion src/Engines/CollectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function searchModels(Builder $builder)
return $models;
}

return $models->filter(function ($model) use ($builder) {
return $models->first()->makeSearchableUsing($models)->filter(function ($model) use ($builder) {
if (! $model->shouldBeSearchable()) {
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ public function map(Builder $builder, $results, $model)
$builder, $objectIds
)->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand All @@ -319,6 +329,16 @@ public function lazyMap(Builder $builder, $results, $model)
$builder, $objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->map(function ($model) use ($results, $objectIdPositions) {
$result = $results['hits'][$objectIdPositions[$model->getScoutKey()]] ?? [];

foreach ($result as $key => $value) {
if (substr($key, 0, 1) === '_') {
$model->withScoutMetadata($key, $value);
}
}

return $model;
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
Expand Down
2 changes: 1 addition & 1 deletion src/Scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Scout
*
* @var string
*/
const VERSION = '10.3.0';
const VERSION = '10.6.0';

/**
* The job class that should make models searchable.
Expand Down
2 changes: 1 addition & 1 deletion src/SearchableScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function extend(EloquentBuilder $builder)

HasManyThrough::macro('unsearchable', function ($chunk = null) {
/** @var HasManyThrough $this */
$this->chunkById($chunk ?: config('scout.chunk.searchable', 500), function ($models) {
$this->chunkById($chunk ?: config('scout.chunk.unsearchable', 500), function ($models) {
$models->unsearchable();

event(new ModelsFlushed($models));
Expand Down
4 changes: 4 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
providers:
- Laravel\Scout\ScoutServiceProvider

migrations: true
14 changes: 5 additions & 9 deletions tests/Feature/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@

use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Scout\EngineManager;
use Laravel\Scout\Engines\MeilisearchEngine;
use Laravel\Scout\ScoutServiceProvider;
use Laravel\Scout\Tests\Fixtures\SearchableUserModel;
use Mockery as m;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;

class BuilderTest extends TestCase
{
use WithFaker;

protected function getPackageProviders($app)
{
return [ScoutServiceProvider::class];
}
use LazilyRefreshDatabase, WithFaker, WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'fake');
}

protected function defineDatabaseMigrations()
protected function afterRefreshingDatabase()
{
$this->setUpFaker();
$this->loadLaravelMigrations();

UserFactory::new()->count(50)->state(new Sequence(function () {
return ['name' => 'Laravel '.$this->faker()->name()];
Expand Down
41 changes: 29 additions & 12 deletions tests/Feature/CollectionEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@

namespace Laravel\Scout\Tests\Feature;

use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Scout\ScoutServiceProvider;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Laravel\Scout\Tests\Fixtures\SearchableModelWithUnloadedValue;
use Laravel\Scout\Tests\Fixtures\SearchableUserModel;
use Laravel\Scout\Tests\Fixtures\SearchableUserModelWithCustomSearchableData;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;

class CollectionEngineTest extends TestCase
{
use WithFaker;

protected function getPackageProviders($app)
{
return [ScoutServiceProvider::class];
}
use LazilyRefreshDatabase, WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'collection');
}

protected function defineDatabaseMigrations()
protected function afterRefreshingDatabase()
{
$this->setUpFaker();
$this->loadLaravelMigrations();

UserFactory::new()->create([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'created_at' => now()->addDay(),
]);

UserFactory::new()->create([
'name' => 'Abigail Otwell',
'email' => '[email protected]',
'created_at' => now()->addDays(2),
]);
}

Expand Down Expand Up @@ -133,4 +130,24 @@ public function test_it_can_order_results()
$this->assertCount(1, $models);
$this->assertEquals('Taylor Otwell', $models[0]->name);
}

public function test_it_can_order_by_latest_and_oldest()
{
$models = SearchableUserModel::search('laravel')->latest()->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Abigail Otwell', $models[0]->name);

$models = SearchableUserModel::search('laravel')->oldest()->paginate(1, 'page', 1);
$this->assertCount(1, $models);
$this->assertEquals('Taylor Otwell', $models[0]->name);
}

public function test_it_calls_make_searchable_using_before_searching()
{
Model::preventAccessingMissingAttributes(true);

$models = SearchableModelWithUnloadedValue::search('loaded')->get();

$this->assertCount(2, $models);
}
}
16 changes: 5 additions & 11 deletions tests/Feature/DatabaseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@

namespace Laravel\Scout\Tests\Feature;

use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Scout\ScoutServiceProvider;
use Laravel\Scout\Tests\Fixtures\SearchableUserDatabaseModel;
use Orchestra\Testbench\Concerns\WithLaravelMigrations;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\Factories\UserFactory;
use Orchestra\Testbench\TestCase;

class DatabaseEngineTest extends TestCase
{
use WithFaker;

protected function getPackageProviders($app)
{
return [ScoutServiceProvider::class];
}
use LazilyRefreshDatabase, WithFaker, WithLaravelMigrations, WithWorkbench;

protected function defineEnvironment($app)
{
$app->make('config')->set('scout.driver', 'database');
}

protected function defineDatabaseMigrations()
protected function afterRefreshingDatabase()
{
$this->setUpFaker();
$this->loadLaravelMigrations();

UserFactory::new()->create([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand Down
7 changes: 2 additions & 5 deletions tests/Feature/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace Laravel\Scout\Tests\Feature;

use Laravel\Scout\ScoutServiceProvider;
use Meilisearch\Client;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

class MeilisearchEngineTest extends TestCase
{
protected function getPackageProviders($app)
{
return [ScoutServiceProvider::class];
}
use WithWorkbench;

protected function defineEnvironment($app)
{
Expand Down
7 changes: 1 addition & 6 deletions tests/Fixtures/SearchableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ class SearchableModel extends Model
*
* @var array
*/
protected $fillable = ['id'];
protected $fillable = ['id', 'name'];

public function searchableAs()
{
return 'table';
}

public function scoutMetadata()
{
return [];
}
}
Loading

0 comments on commit 66934d6

Please sign in to comment.