zing / laravel-scout-opensearch
Laravel Scout custom engine for OpenSearch
Package info
github.com/zingimmick/laravel-scout-opensearch
pkg:composer/zing/laravel-scout-opensearch
Fund package maintenance!
Requires
- php: ^8.2
- ext-json: *
- laravel/scout: ^10.12 || ^11.0
- opensearch-project/opensearch-php: ^2.0
Requires (Dev)
- larastan/larastan: ^3.0
- mockery/mockery: ~1.3.3 || ^1.4.2
- orchestra/testbench: ^10.0 || ^11.0
- phpstan/phpstan-mockery: ^2.0
- phpunit/phpunit: ^11.0 || ^12.0 || ^13.0
- react/promise: ^2.7.1 || ^3.0
- zing/coding-standard: ^7.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- 4.x-dev
- 4.0.0
- 3.x-dev
- 3.5.1
- 3.5.0
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.2.0
- 2.1.0
- 2.0.0
- 1.x-dev
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.0
- 0.0.1
- dev-docs/upgrading-guide
- dev-test/where-value-types
- dev-feature/delete-all-indexes
- dev-feature/scout-indexable-as
- dev-zingimmick-patch-1
- dev-more-checks
This package is auto-updated.
Last update: 2026-11-27 07:26:25 UTC
README
Requires PHP 8.2+
See UPGRADING.md for upgrade instructions.
Require Laravel Scout OpenSearch using Composer:
composer require zing/laravel-scout-opensearch
Configuration
return [ // ... 'opensearch' => [ 'factory' => \OpenSearch\GuzzleClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), 'auth' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')], 'verify' => env('OPENSEARCH_VERIFY_SSL', false), ], 'retries' => env('OPENSEARCH_RETRIES', 2), ], ];
The factory option is optional and defaults to \OpenSearch\GuzzleClientFactory::class. It may be any class that implements \OpenSearch\ClientFactoryInterface and is resolved through Laravel's service container, so its constructor dependencies are injected automatically. The options in client are passed directly to the factory's create() method, so they are specific to the chosen HTTP client. The optional retries and logger options are passed to the factory's maxRetries and logger constructor arguments, which are supported by the built-in Guzzle and Symfony factories.
If opensearch-project/opensearch-php does not provide the factory API (it was introduced in 2.5), the legacy configuration is used automatically. Legacy configuration is also still supported on newer SDK versions through the deprecated ClientBuilder; migrating to the factory configuration above removes the upstream deprecation notices.
Set app name and table name for model
class SearchableModel extends Model { use Searchable; public function searchableAs(): string { return 'searchable_models_index'; } /** * @return array{id: mixed} */ public function toSearchableArray(): array { return [ 'id' => $this->getScoutKey(), ]; } }
Indexing operations use indexableAs (available since Laravel Scout 10.10), which defaults to searchableAs. Override it when writes should go to a different index than searches, for example while swapping indexes:
public function indexableAs(): string { return 'searchable_models_write_index'; }
searchableAs is still used for search queries, while indexableAs is used for indexing, deleting, and flushing records.
Legacy configuration
When the configured SDK version does not provide client factories, or when the factory and client options are not set, the previous configuration is used:
return [ // ... 'opensearch' => [ 'hosts' => [env('OPENSEARCH_HOST', 'localhost:9200')], 'basicAuthentication' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')], 'retries' => env('OPENSEARCH_RETRIES', 2), ], ];
On opensearch-project/opensearch-php 2.4 and later this path uses the deprecated ClientBuilder and emits deprecation notices, so prefer the factory configuration. The factory API used here is available since 2.5.
Using the Symfony HTTP client
Install symfony/http-client and select \OpenSearch\SymfonyClientFactory:
return [ // ... 'opensearch' => [ 'factory' => \OpenSearch\SymfonyClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), 'auth_basic' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')], 'verify_peer' => env('OPENSEARCH_VERIFY_SSL', false), ], 'retries' => env('OPENSEARCH_RETRIES', 2), ], ];
Configuration for Amazon OpenSearch Service
Install aws/aws-sdk-php and use the AWS authentication options supported by the factory:
return [ // ... 'opensearch' => [ 'factory' => \OpenSearch\GuzzleClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), 'auth_aws' => [ 'region' => env('OPENSEARCH_REGION', 'us-east-1'), 'service' => env('OPENSEARCH_SERVICE', 'es'), 'credentials' => [ 'access_key' => env('OPENSEARCH_IAM_KEY'), 'secret_key' => env('OPENSEARCH_IAM_SECRET'), 'session_token' => env('OPENSEARCH_IAM_SESSION_TOKEN'), ], ], ], 'retries' => env('OPENSEARCH_RETRIES', 2), ], ];
Debug and logging
The built-in factories accept a PSR-3 logger through their constructors. Set the top-level logger option to a \Psr\Log\LoggerInterface instance:
use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; return [ // ... 'opensearch' => [ 'factory' => \OpenSearch\GuzzleClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), ], 'retries' => env('OPENSEARCH_RETRIES', 2), 'logger' => (new Logger('opensearch'))->pushHandler( new RotatingFileHandler('opensearch.log') ), ], ];
Custom factory
A custom factory only needs to implement \OpenSearch\ClientFactoryInterface. Its constructor parameters are resolved by Laravel, and the top-level retries and logger options are injected into constructor parameters named maxRetries and logger:
use OpenSearch\Client; use OpenSearch\ClientFactoryInterface; use Psr\Log\LoggerInterface; final class MyOpenSearchClientFactory implements ClientFactoryInterface { public function __construct( protected int $maxRetries = 0, protected ?LoggerInterface $logger = null, ) { } public function create(array $options): Client { return (new \OpenSearch\GuzzleClientFactory($this->maxRetries, $this->logger))->create($options); } }
Then select it in the configuration:
'opensearch' => [ 'factory' => \App\Search\MyOpenSearchClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), ], 'retries' => env('OPENSEARCH_RETRIES', 2), ],
Index settings
The engine implements Scout's \Laravel\Scout\Contracts\UpdatesIndexSettings contract. Configure settings per index under scout.opensearch.index-settings:
return [ // ... 'opensearch' => [ 'factory' => \OpenSearch\GuzzleClientFactory::class, 'client' => [ 'base_uri' => env('OPENSEARCH_URL', 'http://localhost:9200'), ], 'index-settings' => [ 'searchable_models' => [ 'settings' => [ 'number_of_replicas' => 1, 'refresh_interval' => '1s', ], 'mappings' => [ 'properties' => [ 'name' => [ 'type' => 'text', ], ], ], ], ], ], ];
settings are sent with indices()->putSettings() and mappings with indices()->putMapping(). Flat keys outside settings and mappings are also treated as index settings.
Sync the configured settings with:
php artisan scout:sync-index-settings
When Scout soft deletes are enabled, the engine automatically adds an integer mapping for __soft_deleted.
License
Laravel Scout OpenSearch is an open-sourced software licensed under the MIT license.