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

Implement updateIndexSettings for algolia search #829

Closed
yueyuzhao opened this issue May 13, 2024 · 2 comments
Closed

Implement updateIndexSettings for algolia search #829

yueyuzhao opened this issue May 13, 2024 · 2 comments

Comments

@yueyuzhao
Copy link

yueyuzhao commented May 13, 2024

For now only MeilisearchEngine implements updateIndexSettings, but algolia also support index setting, see https://www.algolia.com/doc/api-reference/api-methods/set-settings/.

@driesvints
Copy link
Member

I see you made a PR so let's see how it goes. Thanks

@yueyuzhao
Copy link
Author

Since the PR for this feature is closed, for those who might need this can:

  1. create a custom search engine with method updateIndexSettings and extends AlgoliaEngine
<?php
namespace App;

use Laravel\Scout\Engines\AlgoliaEngine;

class AlgoliaSearchEngine extends AlgoliaEngine {
    /**
     * Update an index's settings.
     *
     * @param string $name
     * @param array $options
     * @return \Algolia\AlgoliaSearch\Response\IndexingResponse
     */
    public function updateIndexSettings(string $name, array $options = [])
    {
        $index = $this->algolia->initIndex($name);

        return $index->setSettings($options);
    }
}
  1. register the search engine, I did this in boot of AppServiceProvider:
<?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'));
        });
    }
}
  1. add index settings in config/scout.php for algolia.
  2. update algolia index settings by running: php artisan scout:sync-index-settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants