novius/laravel-scout-elasticsearch-driver

Elasticsearch Driver for Laravel Scout

3.0.0 2021-06-30 06:29 UTC

This package is auto-updated.

Last update: 2024-03-26 10:11:19 UTC


README

Travis Packagist Release Licence

This package is an adaptation of babenkoivan/scout-elasticsearch-driver to get working with Elasticsearch >= 7.0.0

This package version was created to be compatible with Elasticsearch "Removal of mapping types" introduced in Elasticsearch >= 7.0.0

Features added

  • Model's type is now saved in type field by default according to Elasticsearch recommendations

  • After a model search, an attribute _score will be hydrated on your result Model.

Example :

$results = MyModel::search('keywords')->get();
foreach ($results as $result) {
    // Score is now available in : $result->_score
}
  • Logs : you can now use Laravel loggers to log ElasticSearch's requests

To enable logs you have to set SCOUT_ELASTIC_LOG_ENABLED to true and specify which log's channel(s) to use.

Example :

config/logging.php

<?php

return [

    ...
    
    'channels' => [

        ...

        'es' => [
            'driver' => 'daily',
            'path' => storage_path('logs/es.log'),
            'level' => 'debug',
            'days' => 5,
        ],
    ],
];
  • elastic:reindex a new command to build and populate a new index with 0 downtime ;

config/scout_elastic.php

<?php

return [
    'client' => [
        'hosts' => [
            env('SCOUT_ELASTIC_HOST', 'localhost:9200'),
        ],
    ],
    'document_refresh' => env('SCOUT_ELASTIC_DOCUMENT_REFRESH'),
    'searchable_models' => [],
    'log_enabled' => env('SCOUT_ELASTIC_LOG_ENABLED', false),
    'log_channels' => [],
];

Features deleted from original package

  • elastic:update-mapping command ;
  • elastic:migrate command ;
  • elastic:update command ;
  • Mapping of models : replaced by getDefaultMapping() of index configurator ;
  • Single indexer ;

Requirements

  • PHP >= 7.4
  • Laravel Framework >= 7.25
  • Elasticsearch >= 7.0.0

For Laravel > 6 and < 7.25 you can install 2.x version.

Installation

composer require novius/laravel-scout-elasticsearch-driver:dev-master

Configuration

To configure the package you need to publish settings first:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="Novius\ScoutElastic\ScoutElasticServiceProvider"

Then, set the driver setting to elastic in the config/scout.php file and configure the driver itself in the config/scout_elastic.php file. The available options are:

Option Description
client A setting hash to build Elasticsearch client. More information you can find here. By default the host is set to localhost:9200.
document_refresh This option controls when updated documents appear in the search results. Can be set to 'true', 'false', 'wait_for' or null. More details about this option you can find here. By default set to null.

Note, that if you use the bulk document indexing you'll probably want to change the chunk size, you can do that in the config/scout.php file.

Usage

Please read the original package documentation.

Lint

Run php-cs with:

composer run-script lint

Contributing

Contributions are welcome! Leave an issue on Github, or create a Pull Request.

Licence

This package is under MIT Licence.