Eloquent ORM encapsulation of Elasticsearch for Laravel

5.7 2021-11-02 05:34 UTC


Total Downloads Latest Stable Version Latest Unstable Version License

This is a package to integrate Elasticsearch to Laravel5

It makes you do Elasticsearch just using Eloquent's API.


  1. Require this package with composer:
composer require yong/elasticsuit dev-master
  1. Add service provider to config/app.php
  1. Add elasticsearch node configuration to the "connections" node of config/database.php
        'elasticsearch' => [
            'database'=> 'db*',
            'prefix' => '',
            'settings'=> ['number_of_shards'=>2,'number_of_replicas'=>0]


  1. Define a model for a elasticsearch type
class TestModel extends \Yong\ElasticSuit\Elasticsearch\Model {
    protected $connection = 'elasticsearch';
    protected $table = 'testmodel';

    public function Childmodel () {
        return $this->hasOne(OtherModel::class, '_id');
  1. Create a new document
$testmodel = new TestModel();
$testmodel->first_name = 'firstname';
$testmodel->last_name = 'lastname';
$testmodel->age = 20;
  1. Search a collection
$collection = TestModel::where('first_name', 'like', 'firstname')
    ->whereIn('_id', [1,2,3,4,5])
    ->whereNotIn('_id', [5,6,7,8,9])
    ->where('_id', '=', 1)
    ->where('age', '>', 18)
    ->orWhere('last_name', 'like', 'lastname')
    ->whereMultMatch(['last_name', 'description'], 'search words', '60%')
    ->forPage(1, 20)
    ->select(['first_name', 'last_name', 'age'])

* also support sum(), avg(), min(), max(), stats(), count()
* but not for all fields, only numeric fields can use aggregate
  1. Relations It also support relations, but remember so far just support using default _id as primary key.
    //get relations
    TestModel::with('childmodel')->where('first_name', 'like', 'firstname')->get();


And of course:

MIT: http://rem.mit-license.org