An easy way to use the official Elastic Search client in your Laravel applications.
Install the cviebrock/laravel-elasticsearch
package via composer:
composer require cviebrock/laravel-elasticsearch
Add the service provider and facade (config/app.php
for Laravel 5 or app/config/app.php
for Laravel 4):
'providers' => [
...
Cviebrock\LaravelElasticsearch\ServiceProvider::class, //=> Service provider for Laravel
]
'aliases' => [
...
'Elasticsearch' => Cviebrock\LaravelElasticsearch\Facade::class,
]
Publish the configuration file. For Laravel 5:
php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider"
In order to make this package also work with Laravel 4, we can't do the standard configuration publishing like most Laravel 4 packages do. You will need to simply copy the configuration file into your application's configuration folder:
cp vendor/cviebrock/laravel-elasticsearch/config/elasticsearch.php app/config/
If you work with Lumen, please register the LumenServiceProvider:
$app->register(Cviebrock\LaravelElasticsearch\LumenServiceProvider::class);
The Elasticsearch
facade is just an entry point into the ES client, so previously
you might have used:
$data = [
'body' => [
'testField' => 'abc'
],
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
];
$client = ClientBuilder::create()->build();
$return = $client->index($data);
You can now replace those last two lines with simply:
$return = Elasticsearch::index($data);
That will run the command on the default connection. You can run a command on
any connection (see the defaultConnection
setting and connections
array in
the configuration file).
$return = Elasticsearch::connection('connectionName')->index($data);
Please be noticed that you should not use Facade in Lumen. So, in Lumen - you should use IoC or get the ElasticSearch service object from the application.
$elasticSearch = $this->app('elasticsearch');
Thanks to everyone who has contributed to this project!
Please use Github for bugs, comments, suggestions.
- Fork the project.
- Create your bugfix/feature branch and write your (well-commented) code.
- Create unit tests for your code:
- Run
composer install --dev
in the root directory to install required testing packages. - Add your test methods to
laravel-elasticsearch/tests/
. - Run
vendor/bin/phpunit
to the new (and all previous) tests and make sure everything passes.
- Run
- Commit your changes (and your tests) and push to your branch.
- Create a new pull request against the
master
branch.
Laravel-Elasticsearch was written by Colin Viebrock and released under the MIT License. See the LICENSE file for details.
Copyright 2015 Colin Viebrock