Elastic Search Indexer for Laravel 5.
If any of the following are applicable to your project, then the directory structure should follow industry best practises by being named the following.
config/
src/
tests/
vendor/
Via Composer
$ composer require mabadir/elastic-laravel
Add the ElasticLaravelServiceProvider
to your config/app.php
.
'providers' => [
//Other providers
MAbadir\ElasticLaravel\ElasticLaravelServiceProvider::class,
],
Publish the elastic.php
to your configuration.
$ php artisan vendor:publish
Add the ElasticEloquent trait to your Eloquent model to have it indexed.
namespace App;
use MAbadir\ElasticLaravel\ElasticEloquent;
class User extends Authenticatable
{
use ElasticEloquent;
}
For searching the index, you can run search with different approaches. The first step is to add the Facade to your config/app.php
:
'aliases' => [
//Other Facades
'ElasticSearcher' => MAbadir\ElasticLaravel\ElasticSearcher::class,
],
- Simple term search:
ElasticSearcher::search('simple term');
- Simple term search on specific model type:
$user = App\User::first();
ElasticSearcher::search('Simple Term', $user);
This will search the Elastic Search index for the simple term with type=users
.
- Search index on specific parameter:
ElasticSearcher::search(['name' => 'First Name']);
This will search the complete Search Index for the parameter name with value First Name
- Search index on specific parameter and specific model type:
$user = App\User::first();
ElasticSearcher::search(['name' => 'First Name'], $user);
This will search the Search Index for the parameter name with value First Name
on type=users
.
- Advanced Search:
$params = [
'body' => [
'query' => [
'match' => [
'_all' => 'Simple Term'
]
]
]
];
ElasticSearcher::advanced($params);
This exposes the complete Elastic Search powerful query DSL interface, this will accept any acceptable Elastic Search DSL query.
- Advanced Search:
$user = User::first();
$params = [
'body' => [
'query' => [
'match' => [
'_all' => 'Simple Term'
]
]
]
];
ElasticSearcher::advanced($params, $user);
This will search the index using the advanced query for type=users
.
For the different functions, the class name can be used instead of the object itself, the object or the class should be extending Eloquent Model class \Illuminate\Database\Eloquent\Model
. For example:
ElasticSearcher::search(['name' => 'First Name'], User::class);
By default the package provides a default index initialization command:
$ php artisan es:init
The default command will initialize the index with very basic settings. If it is required to initialize the index with more advanced settings and custom mappings: Create a new Console Command in your application:
$ php artisan make:command IndexIntializationCommand
Import the IndexInitializationTrait
class, and overload the $params
attribute:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use MAbadir\ElasticLaravel\Console\IndexInitializationTrait;
class IndexIntializationCommand extends Command
{
use IndexInitializationTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:initialize';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Initialize ElasticSearch Index';
/**
* Parameters array
*
* @var array
*/
protected $params = [
//Custom Settings
];
}
For more details on the configuration parameters, check the official ElasticSearch documentation.
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.