| Package Data | |
|---|---|
| Maintainer Username: | thaoha |
| Maintainer Contact: | havanthao93@gmail.com (Thao Ha) |
| Package Create Date: | 2016-12-20 |
| Package Last Update: | 2017-01-23 |
| Home Page: | https://thaoha.github.io/eloquent-search/ |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-11-06 15:05:22 |
| Package Statistics | |
|---|---|
| Total Downloads: | 532 |
| Monthly Downloads: | 0 |
| Daily Downloads: | 0 |
| Total Stars: | 0 |
| Total Watchers: | 1 |
| Total Forks: | 1 |
| Total Open Issues: | 1 |
Index Eloquent models to Elasticsearch. Eloquent-search use Official low-level client for Elasticsearch. You should read more about Elasticsearch at https://www.elastic.co to get basic knowledge.
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' => [
...
EloquentEs\EloquentEsServiceProvider::class,
],
Or with Lumen, you need add to bootstrap/app.php:
$app->register(EloquentEs\EloquentEsServiceProvider::class);
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:
$app->configure('elastic');
Create index to store your data first. Use esCreateIndex() function from you model class:
App\Models\Company::esCreateIndex();
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):
App\Models\Company::esPutMapping();
Delete index:
App\Models\Company::esDeleteIndex();
Reset index. Just use this function (this function will delete all your index include your data and create new one with mapping settings):
App\Models\Company::esReset();
With each model object already use ElasticModelTrait you can index to Elasticsearch with esIndex() function:
$company = App\Models\Company::find(1);
$company->esIndex();
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;
}
$company = App\Models\Company::find(1);
$company->esDelete();
$company = App\Models\Company::find(1);
$company->esReindex();
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 https://www.elastic.co/ to build you params search