thaoha / eloquent-search by thaoha

Index Eloquent models to Elasticsearch
Package Data
Maintainer Username: thaoha
Maintainer Contact: (Thao Ha)
Package Create Date: 2016-12-20
Package Last Update: 2017-01-23
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2022-08-07 03:11:02
Package Statistics
Total Downloads: 520
Monthly Downloads: 4
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 1
Total Open Issues: 1


Latest Stable Version License

Index Eloquent models to Elasticsearch. Eloquent-search use Official low-level client for Elasticsearch. You should read more about Elasticsearch at to get basic knowledge.

Installation via Composer

The recommended method to install eloquent-search is through Composer.

composer require thaoha/eloquent-search

Once you've run a composer update, you need to register Laravel service provider, in your config/app.php:

'providers' => [

Or with Lumen, you need add to bootstrap/app.php:


And now you can add ElasticModelTrait to any Eloquent model you want to index to Elasticsearch:

use EloquentEs\Supports\ElasticModelTrait;

 * Class Company
 * @package App\Models
class Company extends Model
    use ElasticModelTrait;


Laravel 5:

$ php artisan vendor:publish --provider="EloquentEs\EloquentEsServiceProvider"

Or you can copy config.php file to your config folder and change the filename to elastic.php. With Lumen you need add new config file to bootstrap/app:



Create index to store your data first. Use esCreateIndex() function from you model class:


esCreateIndex() function use property $esIndexMapping in Company model to set mapping settings. Elastic will auto detect if $esIndexMapping empty:

     * Index mapping
     * @var array
    private $esIndexMapping = [
        'id'            => ['type' => 'long'],
        'name'          => ['type' => 'string'],
        'company'       => ['type' => 'string']

If you want to update mapping settings you can use (use esReset() function when conflict error):


Delete index:


Reset index. Just use this function (this function will delete all your index include your data and create new one with mapping settings):


Get and Index model

With each model object already use ElasticModelTrait you can index to Elasticsearch with esIndex() function:

$company = App\Models\Company::find(1);

With default it will be use $company->toArray() to get data. Very easy to override with esSerialize() function:

     * Get eloquent model data
     * @return array
    public function esSerialize()
        $data = $this->toArray();
        $data['user_id'] = 1;
        return $data;

Delete model

$company = App\Models\Company::find(1);

Update model

$company = App\Models\Company::find(1);

a Model or a Collection you can do with same way.


$params = [
    'match' => ['name' => 'keyword']
$hits = App\Models\Company::esSearch($params);

You should read more at to build you params search