Published 4 years ago.
Author Peter Steenbergen
Hi, this is the first of many blog posts in a series of using Elasticsearch with Laravel.
In this article my focus is to setup a blank Laravel project and install the official Elasticsearch php package and hooking it into a Service Provider so we can change the environment later in production without changing the code itself.
Create a new laravel project
laravel new laravelelastic
Adding Elasticsearch repository
Now in the project folder add the composer package to the Application.
cd laravelelastic
composer require elasticsearch/elasticsearch
Setting up the Service Provider
Now that the Elasticsearch package is installed we can setup a connection through a Service Provider. So when a class needs the Elastic/Client's class we generate a Client using the ClientBuilder and add our configuration to the service provider.
We will start by creating a config file for elasticsearch at config/elasticsearch.php.
<?php
return [
'hosts' => [
[
'host' => env('ELASTICSEARCH_HOST', 'localhost'),
'user' => env('ELASTICSEARCH_USER', ''),
'pass' => env('ELASTICSEARCH_PASS', ''),
],
],
];
We want to ingest this configuration through the AppServiceProvider. Open app/Providers/AppServiceProvider.php and set the contents to:
<?php
namespace App\Providers;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Client::class, function () {
return ClientBuilder::create()
->setHosts(config('elasticsearch.hosts'))
->build();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
Testing the connection
Let's play some ping-pong shall we? If we send a ping to elasticsearch to see if the service is running keep we should output "pong".
First we will make a command in our Application.
php artisan make:command ElasticsearchPing
Open the file and add the following contents.
<?php
namespace App\Console\Commands;
use Elasticsearch\Client;
use Illuminate\Console\Command;
class ElasticsearchPing extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'elasticsearch:ping';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Ping Elasticsearch';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @param Client $client
*/
public function handle(Client $client)
{
if ($client->ping()) {
$this->info('pong');
return;
}
$this->error('Could not connect to Elasticsearch.');
}
}
If all went well the output of the following command should be "pong".
php artisan elasticsearch:ping
Closing
So we now have a Laravel Project up and running with the latest Elasticsearch Client. Our next blog post will be setting up an index and attach some models to it.