Closed yueyuzhao closed 3 months ago
I see you made a PR so let's see how it goes. Thanks
Since the PR for this feature is closed, for those who might need this can:
updateIndexSettings
and extends AlgoliaEngine
<?php
namespace App;
use Laravel\Scout\Engines\AlgoliaEngine;
class AlgoliaSearchEngine extends AlgoliaEngine { /**
@return \Algolia\AlgoliaSearch\Response\IndexingResponse */ public function updateIndexSettings(string $name, array $options = []) { $index = $this->algolia->initIndex($name);
return $index->setSettings($options);
} }
2. register the search engine, I did this in `boot` of `AppServiceProvider`:
```php
<?php
namespace App\Providers;
use Algolia\AlgoliaSearch\SearchClient as Algolia; use Algolia\AlgoliaSearch\Config\SearchConfig; use Algolia\AlgoliaSearch\Support\UserAgent; use App\AlgoliaSearchEngine; use Illuminate\Support\ServiceProvider; use Laravel\Scout\EngineManager; use Laravel\Scout\Scout;
class AlgoliaServiceProvider extends ServiceProvider { /**
Register services. */ public function register(): void { // }
/**
Bootstrap services. */ public function boot(): void { resolve(EngineManager::class)->extend('algolia', function () { UserAgent::addCustomUserAgent('Laravel Scout', Scout::VERSION);
$defaultAlgoliaHeaders = function () {
if (!config('scout.identify')) {
return [];
}
$headers = [];
if (!config('app.debug') &&
filter_var($ip = request()->ip(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
) {
$headers['X-Forwarded-For'] = $ip;
}
if (($user = request()->user()) && method_exists($user, 'getKey')) {
$headers['X-Algolia-UserToken'] = $user->getKey();
}
return $headers;
};
$config = SearchConfig::create(
config('scout.algolia.id'),
config('scout.algolia.secret')
)->setDefaultHeaders(
$defaultAlgoliaHeaders()
);
if (is_int($connectTimeout = config('scout.algolia.connect_timeout'))) {
$config->setConnectTimeout($connectTimeout);
}
if (is_int($readTimeout = config('scout.algolia.read_timeout'))) {
$config->setReadTimeout($readTimeout);
}
if (is_int($writeTimeout = config('scout.algolia.write_timeout'))) {
$config->setWriteTimeout($writeTimeout);
}
if (is_int($batchSize = config('scout.algolia.batch_size'))) {
$config->setBatchSize($batchSize);
}
return new AlgoliaSearchEngine(Algolia::createWithConfig($config), config('scout.soft_delete'));
});
} }
4. add index settings in `config/scout.php` for algolia.
5. update algolia index settings by running: `php artisan scout:sync-index-settings`
For now only
MeilisearchEngine
implementsupdateIndexSettings
, but algolia also support index setting, see https://www.algolia.com/doc/api-reference/api-methods/set-settings/.