mabadir/elastic-laravel

This package is abandoned and no longer maintained. No replacement package was suggested.

Elastic Search Indexer for Laravel 5

1.1.4 2017-08-24 15:08 UTC

This package is auto-updated.

Last update: 2019-09-24 14:35:33 UTC


README

Latest Version on Packagist Software License Quality Score Total Downloads

Elastic Search Indexer for Laravel 5.

Structure

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/

Install

Via Composer

$ composer require mabadir/elastic-laravel

Usage

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,
],
  1. Simple term search:
ElasticSearcher::search('simple term');
  1. 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.

  1. 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

  1. 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.

  1. 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.

  1. 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);

ElasticSearch Indexing Console Command

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.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email mina@abadir.email instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.