This laravel tutorial help to integrate and build a custom search engine based on Elasticsearch. We will integrate Elasticsearch nosql database with laravel and fetch data using Elasticsearch query.
Laravel is the most popular PHP framework and is easy to use. Elasticsearch allows you to search & analyze data in real-time. The composer helps to install project dependencies into laravel in a gentle way. We just add the below entry into the composer.json
to install Elasticsearch plugin.
{ "require": { "elasticsearch/elasticsearch": "~7.0" "cviebrock/laravel-elasticsearch": "^2" } }
And now run the below command –
php composer install
I am using cviebrock/laravel-elasticsearch package, This help to use the official Elastic Search client in your Laravel applications.
I am assuming, You have an Elasticsearch index and credentials, We just connect Elasticsearch with laravel and get the data from there. You can get started with laravel by following Laravel 9 Installation And Example.
Elasticsearch has an index to store search engine data. An index is like a table in a relational database. It has a mapping, which defines the field types.
The package’s service provider will automatically register its service provider.
Let’s publish the configuration file using the following command –
php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider"
Now go to the config file at app >> config >> elasticsearch.php and add the default index name –
'default_index' => 'employees'
The laravel application has a .env
file that will use for configuration with Elasticsearch. Open .env
file and updated below entries –
ELASTICSEARCH_HOST=localhost ELASTICSEARCH_PORT=9200 ELASTICSEARCH_SCHEME=http ELASTICSEARCH_USER= ELASTICSEARCH_PASS=
We will use ClientBuilder method to create an es client into the laravel, I also need an Elasticsearch host to connect with them.
use Elasticsearch\ClientBuilder; public function _ESCLient { $es_host = 'ES Host'; $this->es = ClientBuilder::create() // Instantiate a new ClientBuilder ->setHosts($es_host) // Set the hosts ->build(); }
Let’s fetch data from elasticsearch using ESClient
, We will create all()
method that will take $from and $size as a parameter and passed to elasticsearch.
public function all($from = 0, $size = 200) { $es = $this->es; $params['index'] = $this->index; $params['type'] = $this->type; $params['size'] = $size; $params['from'] = $from; try { $results = $es->search($params); $hits = $results['hits']; return $hits; } catch(\Exception $ex) { \Log::critical($ex); $this->apiError("ES - Unable to get Entries", 400); } }
Where $this->index
is the elasticsearch index name and $this->type
is the type of the doc.
This tutorial help to demonstrate in detail how to Connect Elasticsearch with Laravel.We have configured Elasticsearch with laravel 9 and fetched all data.
If you have any questions regarding this laravel 9 article, or with the configuration of Elasticsearch in Laravel, feel free to mention them in the comment section below.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More