Skip to content

Getting Started

An OpenSearch Implementation of Laravel’s Eloquent ORM

Section titled “An OpenSearch Implementation of Laravel’s Eloquent ORM”

This package seamlessly integrates OpenSearch functionalities into Laravel’s Eloquent model and query builder, making it feel native to Laravel. This enables you to utilize Eloquent models while leveraging the powerful search and analytics capabilities of OpenSearch.


Laravel 10/11/12

Terminal window
composer require pdphilip/opensearch

  1. Set up your .env with the following OpenSearch settings:

    OS_HOSTS="http://opensearch:9200"
    OS_USERNAME=
    OS_PASSWORD=
    OS_INDEX_PREFIX=my_app_
    # prefix will be added to all indexes created by the package with an underscore
    # ex: my_app_user_logs for UserLog.php model
    # AWS SigV4 Config:
    OS_SIG_V4_PROVIDER=
    OS_SIG_V4_REGION=
    OS_SIG_V4_SERVICE=
    # Cert Config:
    OS_SSL_CERT=
    OS_SSL_CERT_PASSWORD=
    OS_SSL_KEY=
    OS_SSL_KEY_PASSWORD=
    # Optional Settings:
    OS_OPT_VERIFY_SSL=true
    OS_OPT_RETRIES=
    OS_OPT_SNIFF_ON_START=
    OS_OPT_PORT_HOST_HEADERS=
    OS_OPT_ID_SORTABLE=false
    OS_OPT_META_HEADERS=true
    OS_OPT_BYPASS_MAP_VALIDATION=false
    OS_OPT_DEFAULT_LIMIT=1000

    For multiple nodes, pass in as comma-separated:

    OS_HOSTS="http://opensearch-node1:9200,http://opensearch-node2:9200,http://opensearch-node3:9200"
  2. In config/database.php, add the opensearch connection:

    'opensearch' => [
    'driver' => 'opensearch',
    'hosts' => explode(',', env('OS_HOSTS', 'http://localhost:9200')),
    'basic_auth' => [
    'username' => env('OS_USERNAME', ''),
    'password' => env('OS_PASSWORD', ''),
    ],
    'sig_v4' => [
    'provider' => env('OS_SIG_V4_PROVIDER'),
    'region' => env('OS_SIG_V4_REGION'),
    'service' => env('OS_SIG_V4_SERVICE'),
    ],
    'ssl' => [
    'cert' => env('OS_SSL_CERT', ''),
    'cert_password' => env('OS_SSL_CERT_PASSWORD', ''),
    'key' => env('OS_SSL_KEY', ''),
    'key_password' => env('OS_SSL_KEY_PASSWORD', ''),
    ],
    'index_prefix' => env('OS_INDEX_PREFIX', false),
    'options' => [
    'bypass_map_validation' => env('OS_OPT_BYPASS_MAP_VALIDATION', false),
    'ssl_verification' => env('OS_OPT_VERIFY_SSL', true),
    'retires' => env('OS_OPT_RETRIES',null),
    'sniff_on_start' => env('OS_OPT_SNIFF_ON_START',false),
    'logging' => env('OS_OPT_LOGGING', false),
    'port_in_host_header' => env('OS_OPT_PORT_HOST_HEADERS',false),
    'default_limit' => env('OS_OPT_DEFAULT_LIMIT', 1000),
    'allow_id_sort' => env('OS_OPT_ID_SORTABLE', false),
    ],
    ],
  3. If packages are not autoloaded, add the service provider: For Laravel 10 and below:

    config/app.php
    'providers' => [
    ...
    ...
    PDPhilip\OpenSearch\ElasticServiceProvider::class,
    ...

    For Laravel 11 & 12:

    bootstrap/providers.php
    <?php
    return [
    App\Providers\AppServiceProvider::class,
    PDPhilip\OpenSearch\ElasticServiceProvider::class,
    ];

This setup prepares you to fully utilize OpenSearch within Laravel, treating it as an integral part of the framework.

Laravel VersionCommandMaintenance
Laravel 10/11/12composer require pdphilip/opensearch✅ Active
Laravel 10 & 11 (v2)composer require pdphilip/opensearch:~2❌ EOL
Laravel 8 & 9composer require pdphilip/opensearch:~1❌ EOL