Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports for algolia/algoliasearch-client-php v4 #872

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ jobs:
composer update --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --group external-network,meilisearch
run: vendor/bin/phpunit --no-configuration --no-coverage --color --bootstrap vendor/autoload.php --group meilisearch tests/Integration
env:
MEILISEARCH_HOST: "http://localhost:7700"
MEILISEARCH_KEY: 'masterKey'
DB_CONNECTION: 'testing'
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/console": "^6.0|^7.0"
},
"require-dev": {
"algolia/algoliasearch-client-php": "^3.2",
"algolia/algoliasearch-client-php": "^4.0",
"typesense/typesense-php": "^4.9.3",
"meilisearch/meilisearch-php": "^1.0",
"mockery/mockery": "^1.0",
Expand Down
11 changes: 6 additions & 5 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Laravel\Scout;

use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Support\UserAgent;
use Algolia\AlgoliaSearch\Api\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
use Algolia\AlgoliaSearch\Model\Ingestion\Event;
use Algolia\AlgoliaSearch\Support\AlgoliaAgent;
use Exception;
use Illuminate\Support\Manager;
use Laravel\Scout\Engines\AlgoliaEngine;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function createAlgoliaDriver()
{
$this->ensureAlgoliaClientIsInstalled();

UserAgent::addCustomUserAgent('Laravel Scout', Scout::VERSION);
AlgoliaAgent::addAlgoliaAgent('Laravel Scout', 'Laravel Scout', Scout::VERSION);

$config = SearchConfig::create(
config('scout.algolia.id'),
Expand All @@ -61,7 +62,7 @@ public function createAlgoliaDriver()
}

if (is_int($batchSize = config('scout.algolia.batch_size'))) {
$config->setBatchSize($batchSize);
(new Event())->setBatchSize($batchSize);
crynobone marked this conversation as resolved.
Show resolved Hide resolved
}

return new AlgoliaEngine(Algolia::createWithConfig($config), config('scout.soft_delete'));
Expand Down
34 changes: 16 additions & 18 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Scout\Engines;

use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Api\SearchClient as Algolia;
use Exception;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\LazyCollection;
Expand All @@ -14,7 +14,7 @@ class AlgoliaEngine extends Engine
/**
* The Algolia client.
*
* @var \Algolia\AlgoliaSearch\SearchClient
* @var \Algolia\AlgoliaSearch\Api\SearchClient
*/
protected $algolia;

Expand All @@ -28,7 +28,7 @@ class AlgoliaEngine extends Engine
/**
* Create a new engine instance.
*
* @param \Algolia\AlgoliaSearch\SearchClient $algolia
* @param \Algolia\AlgoliaSearch\Api\SearchClient $algolia
* @param bool $softDelete
* @return void
*/
Expand All @@ -52,7 +52,7 @@ public function update($models)
return;
}

$index = $this->algolia->initIndex($models->first()->indexableAs());
$index = $models->first()->indexableAs();

if ($this->usesSoftDelete($models->first()) && $this->softDelete) {
$models->each->pushSoftDeleteMetadata();
Expand All @@ -71,7 +71,7 @@ public function update($models)
})->filter()->values()->all();

if (! empty($objects)) {
$index->saveObjects($objects);
$this->algolia->saveObjects($index, $objects);
}
}

Expand All @@ -87,13 +87,11 @@ public function delete($models)
return;
}

$index = $this->algolia->initIndex($models->first()->indexableAs());

$keys = $models instanceof RemoveableScoutCollection
? $models->pluck($models->first()->getScoutKeyName())
: $models->map->getScoutKey();

$index->deleteObjects($keys->all());
$this->algolia->deleteObjects($models->first()->indexableAs(), $keys->all());
}

/**
Expand Down Expand Up @@ -136,22 +134,24 @@ public function paginate(Builder $builder, $perPage, $page)
*/
protected function performSearch(Builder $builder, array $options = [])
{
$algolia = $this->algolia->initIndex(
$builder->index ?: $builder->model->searchableAs()
);

$options = array_merge($builder->options, $options);

if ($builder->callback) {
return call_user_func(
$builder->callback,
$algolia,
$this->algolia,
$builder->query,
$options
);
}

return $algolia->search($builder->query, $options);
$queryParams = ['query' => $builder->query];

return $this->algolia->searchSingleIndex(
$builder->index ?: $builder->model->searchableAs(),
$queryParams,
$options
);
}

/**
Expand Down Expand Up @@ -280,9 +280,7 @@ public function getTotalCount($results)
*/
public function flush($model)
{
$index = $this->algolia->initIndex($model->indexableAs());

$index->clearObjects();
$this->algolia->clearObjects($model->indexableAs());
}

/**
Expand All @@ -307,7 +305,7 @@ public function createIndex($name, array $options = [])
*/
public function deleteIndex($name)
{
return $this->algolia->initIndex($name)->delete();
return $this->algolia->deleteIndex($name);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions tests/Integration/AlgoliaSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Laravel\Scout\Tests\Integration;

use Illuminate\Support\Env;
use Laravel\Scout\Tests\Fixtures\User;
use Orchestra\Testbench\Attributes\RequiresEnv;

/**
* @group algolia
* @group external-network
*/
#[RequiresEnv('ALGOLIA_APP_ID')]
class AlgoliaSearchableTest extends TestCase
{
use SearchableTests;
Expand All @@ -21,10 +22,6 @@ class AlgoliaSearchableTest extends TestCase
*/
protected function defineEnvironment($app)
{
if (is_null(Env::get('ALGOLIA_APP_ID'))) {
$this->markTestSkipped();
}

$this->defineScoutEnvironment($app);
}

Expand Down
9 changes: 2 additions & 7 deletions tests/Integration/MeilisearchSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
namespace Laravel\Scout\Tests\Integration;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Env;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\MeilisearchEngine;
use Laravel\Scout\Tests\Fixtures\User;
use Laravel\Scout\Tests\Fixtures\VersionableModel;
use Meilisearch\Client;
use Meilisearch\Endpoints\Indexes;
use Mockery as m;
use Orchestra\Testbench\Attributes\RequiresEnv;

/**
* @group meilisearch
* @group external-network
*/
#[RequiresEnv('MEILISEARCH_HOST')]
class MeilisearchSearchableTest extends TestCase
{
use SearchableTests {
Expand All @@ -30,12 +31,6 @@ class MeilisearchSearchableTest extends TestCase
*/
protected function defineEnvironment($app)
{
if (is_null(Env::get('MEILISEARCH_HOST'))) {
$this->markTestSkipped();

return;
}

$this->defineScoutEnvironment($app);
}

Expand Down
7 changes: 2 additions & 5 deletions tests/Integration/TypesenseSearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Laravel\Scout\Tests\Integration;

use Illuminate\Support\Env;
use Laravel\Scout\Tests\Fixtures\User;
use Orchestra\Testbench\Attributes\RequiresEnv;

/**
* @group typesense
* @group external-network
*/
#[RequiresEnv('TYPESENSE_API_KEY')]
class TypesenseSearchableTest extends TestCase
{
use SearchableTests;
Expand All @@ -21,10 +22,6 @@ class TypesenseSearchableTest extends TestCase
*/
protected function defineEnvironment($app)
{
if (is_null(Env::get('TYPESENSE_API_KEY'))) {
$this->markTestSkipped();
}

$this->defineScoutEnvironment($app);
}

Expand Down
44 changes: 21 additions & 23 deletions tests/Unit/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Scout\Tests\Unit;

use Algolia\AlgoliaSearch\SearchClient;
use Algolia\AlgoliaSearch\Api\SearchClient;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Config;
Expand Down Expand Up @@ -48,8 +48,7 @@ public function test_update_adds_objects_to_index()
public function test_delete_removes_objects_to_index()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('deleteObjects')->with([1]);
$client->shouldReceive('deleteObjects')->with('table', [1]);

$engine = new AlgoliaEngine($client);
$engine->delete(Collection::make([new SearchableModel(['id' => 1])]));
Expand All @@ -58,8 +57,7 @@ public function test_delete_removes_objects_to_index()
public function test_delete_removes_objects_to_index_with_a_custom_search_key()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('deleteObjects')->once()->with(['my-algolia-key.5']);
$client->shouldReceive('deleteObjects')->once()->with('table', ['my-algolia-key.5']);

$engine = new AlgoliaEngine($client);
$engine->delete(Collection::make([new AlgoliaCustomKeySearchableModel(['id' => 5])]));
Expand All @@ -74,8 +72,7 @@ public function test_delete_with_removeable_scout_collection_using_custom_search
$job = unserialize(serialize($job));

$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('deleteObjects')->once()->with(['my-algolia-key.5']);
$client->shouldReceive('deleteObjects')->once()->with('table', ['my-algolia-key.5']);

$engine = new AlgoliaEngine($client);
$engine->delete($job->models);
Expand Down Expand Up @@ -111,10 +108,11 @@ public function test_remove_from_search_job_uses_custom_search_key()
public function test_search_sends_correct_parameters_to_algolia()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1'],
]);
$client->shouldReceive('searchSingleIndex')->with(
'table',
['query' => 'zonda'],
['numericFilters' => ['foo=1']]
);

$engine = new AlgoliaEngine($client);
$builder = new Builder(new SearchableModel, 'zonda');
Expand All @@ -125,10 +123,11 @@ public function test_search_sends_correct_parameters_to_algolia()
public function test_search_sends_correct_parameters_to_algolia_for_where_in_search()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1', ['bar=1', 'bar=2']],
]);
$client->shouldReceive('searchSingleIndex')->with(
'table',
['query' => 'zonda'],
['numericFilters' => ['foo=1', ['bar=1', 'bar=2']]]
);

$engine = new AlgoliaEngine($client);
$builder = new Builder(new SearchableModel, 'zonda');
Expand All @@ -139,10 +138,11 @@ public function test_search_sends_correct_parameters_to_algolia_for_where_in_sea
public function test_search_sends_correct_parameters_to_algolia_for_empty_where_in_search()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1', '0=1'],
]);
$client->shouldReceive('searchSingleIndex')->with(
'table',
['query' => 'zonda'],
['numericFilters' => ['foo=1', '0=1']]
);

$engine = new AlgoliaEngine($client);
$builder = new Builder(new SearchableModel, 'zonda');
Expand Down Expand Up @@ -280,8 +280,7 @@ public function test_a_model_is_indexed_with_a_custom_algolia_key()
public function test_a_model_is_removed_with_a_custom_algolia_key()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('deleteObjects')->with(['my-algolia-key.1']);
$client->shouldReceive('deleteObjects')->with('table', ['my-algolia-key.1']);

$engine = new AlgoliaEngine($client);
$engine->delete(Collection::make([new AlgoliaCustomKeySearchableModel(['id' => 1])]));
Expand All @@ -290,8 +289,7 @@ public function test_a_model_is_removed_with_a_custom_algolia_key()
public function test_flush_a_model_with_a_custom_algolia_key()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('clearObjects');
$client->shouldReceive('clearObjects')->with('table');

$engine = new AlgoliaEngine($client);
$engine->flush(new AlgoliaCustomKeySearchableModel);
Expand Down