Skip to content

gazzoy/laravel-scout-opensearch

 
 

Repository files navigation

Laravel Scout OpenSearch

Build Status Code Coverage Latest Stable Version Total Downloads Latest Unstable Version License

Requires PHP 8.0+

Require Laravel Scout OpenSearch using Composer:

composer require gazzoy/laravel-scout-opensearch

Configuration

return [
    // ...
    'opensearch' => [
        'client' => [
            'hosts' => [env('OPENSEARCH_HOST', 'localhost:9200')],
            'basicAuthentication' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')],
            'retries' => env('OPENSEARCH_RETRYS', 2),
        ]
    ],
];

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(),
        ];
    }
}

Configuration for Amazon OpenSearch Service

Use indices param if you want to turn on index mappings.

return [
    // ...
    'opensearch' => [
        'client' => [
            'hosts' => [env('OPENSEARCH_HOST', 'localhost:9200')],
            'basicAuthentication' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')],
            'retries' => env('OPENSEARCH_RETRYS', 2),
            'sigV4Region' => env('OPENSEARCH_REGION', 'us-east-1') ,
            'sigV4Service' => env('OPENSEARCH_SERVICE', 'es') ,
            'sigV4CredentialProvider' => [
                'key' => env('OPENSEARCH_IAM_KEY'),
                'secret' => env('OPENSEARCH_IAM_SECRET'),
            ],
        ],
//    'indices' => [
//        'default' => [
//            'settings' => [
//                'index' => [
//                    'number_of_shards' => 3,
//                ],
//            ],
//        ],
//        'table' => [
//            'mappings' => [
//                'properties' => [
//                    'id' => [
//                        'type' => 'keyword',
//                    ],
//                ],
//            ],
//        ],
    ],
];

Debug and logging

return [
    // ...
    'opensearch' => [
        'client' => [
            'hosts' => [env('OPENSEARCH_HOST', 'localhost:9200')],
            'basicAuthentication' => [env('OPENSEARCH_USERNAME', 'admin'), env('OPENSEARCH_PASSWORD', 'admin')],
            'retries' => env('OPENSEARCH_RETRYS', 2),
            'logger' => (new \Monolog\Logger('opensearch'))->pushHandler(new \Monolog\Handler\RotatingFileHandler('opensearch.log')),
            'tracer' => (new \Monolog\Logger('opensearch'))->pushHandler(new \Monolog\Handler\RotatingFileHandler('opensearch.log')),
        ]
    ],
];

Test

$ composer update
$ composer check-platform-reqs
$ docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" -e "plugins.security.disabled=true" opensearchproject/opensearch:latest
$ composer fix
$ ./vendor/bin/phpunit

License

Laravel Scout OpenSearch is an open-sourced software licensed under the MIT license.

About

Laravel Scout custom engine for OpenSearch

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%