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 all 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
8 changes: 3 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
php: ['8.0', 8.1, 8.2, 8.3]
laravel: [9, 10, 11]
exclude:
- php: '8.0'
laravel: 9
- php: '8.0'
laravel: 10
- php: '8.0'
Expand All @@ -47,8 +45,7 @@ jobs:

- name: Install dependencies
run: |
composer require "illuminate/contracts=^${{ matrix.laravel }}" --no-update
composer update --prefer-dist --no-interaction --no-progress
composer update --prefer-dist --no-interaction --no-progress --with="illuminate/contracts=^${{ matrix.laravel }}"

- name: Execute tests
run: vendor/bin/phpunit
Expand Down Expand Up @@ -90,7 +87,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'
5 changes: 4 additions & 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": "^3.2|^4.0",
"typesense/typesense-php": "^4.9.3",
"meilisearch/meilisearch-php": "^1.0",
"mockery/mockery": "^1.0",
Expand All @@ -34,6 +34,9 @@
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.3|^10.4"
},
"conflict": {
"algolia/algoliasearch-client-php": "<3.2.0|>=5.0.0"
},
"autoload": {
"psr-4": {
"Laravel\\Scout\\": "src/"
Expand Down
4 changes: 4 additions & 0 deletions phpstan.src.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ parameters:
level: 0

ignoreErrors:
- identifier: new.static
- "#\\(void\\) is used.#"

excludePaths:
- src/Engines/Algolia3Engine.php
66 changes: 36 additions & 30 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Laravel\Scout;

use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Support\UserAgent;
use Algolia\AlgoliaSearch\Algolia;
use Algolia\AlgoliaSearch\Support\AlgoliaAgent as Algolia4UserAgent;
use Algolia\AlgoliaSearch\Support\UserAgent as Algolia3UserAgent;
use Exception;
use Illuminate\Support\Manager;
use Laravel\Scout\Engines\AlgoliaEngine;
use Laravel\Scout\Engines\Algolia3Engine;
use Laravel\Scout\Engines\Algolia4Engine;
use Laravel\Scout\Engines\CollectionEngine;
use Laravel\Scout\Engines\DatabaseEngine;
use Laravel\Scout\Engines\MeilisearchEngine;
Expand Down Expand Up @@ -39,32 +40,41 @@ public function createAlgoliaDriver()
{
$this->ensureAlgoliaClientIsInstalled();

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

$config = SearchConfig::create(
config('scout.algolia.id'),
config('scout.algolia.secret')
)->setDefaultHeaders(
$this->defaultAlgoliaHeaders()
);

if (is_int($connectTimeout = config('scout.algolia.connect_timeout'))) {
$config->setConnectTimeout($connectTimeout);
}
return version_compare(Algolia::VERSION, '4.0.0', '>=')
? $this->configureAlgolia4Driver()
: $this->configureAlgolia3Driver();
}

if (is_int($readTimeout = config('scout.algolia.read_timeout'))) {
$config->setReadTimeout($readTimeout);
}
/**
* Create an Algolia v3 engine instance.
*
* @return \Laravel\Scout\Engines\Algolia3Engine
*/
protected function configureAlgolia3Driver()
{
Algolia3UserAgent::addCustomUserAgent('Laravel Scout', Scout::VERSION); // @phpstan-ignore class.notFound

if (is_int($writeTimeout = config('scout.algolia.write_timeout'))) {
$config->setWriteTimeout($writeTimeout);
}
return Algolia3Engine::make(
config: config('scout.algolia'),
headers: $this->defaultAlgoliaHeaders(),
softDelete: config('scout.soft_delete')
);
}

if (is_int($batchSize = config('scout.algolia.batch_size'))) {
$config->setBatchSize($batchSize);
}
/**
* Create an Algolia v4 engine instance.
*
* @return \Laravel\Scout\Engines\Algolia4Engine
*/
protected function configureAlgolia4Driver()
{
Algolia4UserAgent::addAlgoliaAgent('Laravel Scout', 'Laravel Scout', Scout::VERSION);

return new AlgoliaEngine(Algolia::createWithConfig($config), config('scout.soft_delete'));
return Algolia4Engine::make(
config: config('scout.algolia'),
headers: $this->defaultAlgoliaHeaders(),
softDelete: config('scout.soft_delete')
);
}

/**
Expand All @@ -80,10 +90,6 @@ protected function ensureAlgoliaClientIsInstalled()
return;
}

if (class_exists('AlgoliaSearch\Client')) {
throw new Exception('Please upgrade your Algolia client to version: ^3.2.');
}

throw new Exception('Please install the suggested Algolia client: algolia/algoliasearch-client-php.');
}

Expand Down
169 changes: 169 additions & 0 deletions src/Engines/Algolia3Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Laravel\Scout\Engines;

use Algolia\AlgoliaSearch\Config\SearchConfig as Algolia3SearchConfig;
use Algolia\AlgoliaSearch\SearchClient as Algolia3SearchClient;
use Laravel\Scout\Builder;
use Laravel\Scout\Jobs\RemoveableScoutCollection;

/**
* @template TAlgoliaClient of \Algolia\AlgoliaSearch\SearchClient
*/
class Algolia3Engine extends AlgoliaEngine
{
/**
* Create a new engine instance.
*
* @param TAlgoliaClient $algolia
* @param bool $softDelete
* @return void
*/
public function __construct(Algolia3SearchClient $algolia, $softDelete = false)
{
parent::__construct($algolia, $softDelete);
}

/**
* Make a new engine instance.
*
* @param array $config
* @param array $headers
* @param bool $softDelete
* @return static
*/
public static function make(array $config, array $headers, bool $softDelete = false)
{
$config = Algolia3SearchConfig::create([
'appId' => $config['id'],
'apiKey' => $config['secret'],
])->setDefaultHeaders($headers);

if (is_int($connectTimeout = $config['connect_timeout'])) {
$configuration->setConnectTimeout($connectTimeout);
}

if (is_int($readTimeout = $config['read_timeout'])) {
$configuration->setReadTimeout($readTimeout);
}

if (is_int($writeTimeout = $config['write_timeout'])) {
$configuration->setWriteTimeout($writeTimeout);
}

if (is_int($batchSize = $config['batch_size'])) {
$configuration->setBatchSize($batchSize);
}

return new static(Algolia3SearchClient::createWithConfig($configuration), $softDelete);
}

/**
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*
* @throws \Algolia\AlgoliaSearch\Exceptions\AlgoliaException
*/
public function update($models)
{
if ($models->isEmpty()) {
return;
}

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

if ($this->usesSoftDelete($models->first()) && $this->softDelete) {
$models->each->pushSoftDeleteMetadata();
}

$objects = $models->map(function ($model) {
if (empty($searchableData = $model->toSearchableArray())) {
return;
}

return array_merge(
$searchableData,
$model->scoutMetadata(),
['objectID' => $model->getScoutKey()],
);
})->filter()->values()->all();

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

/**
* Remove the given model from the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function delete($models)
{
if ($models->isEmpty()) {
return;
}

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

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

$index->deleteObjects($keys->all());
}

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
public function deleteIndex($name)
{
return $this->algolia->initIndex($name)->delete();
}

/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function flush($model)
{
$index = $this->algolia->initIndex($model->indexableAs());

$index->clearObjects();
}

/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param array $options
* @return mixed
*/
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,
$builder->query,
$options
);
}

return $algolia->search($builder->query, $options);
}
}
Loading