ElasticSearch Example with Laravel 7

This laravel tutorial help to integrate build a custom search engine based on Elasticsearch. We will integrate Elasticsearch nosql database with laravel and fetch data using Elasticsearch query.

The laravel is most popular php framework and easy to use. The Elasticsearch allows you to search & analyze data in real time.The composer help to install project dependencies into laravel in a gentle way.We just add below entry into the composer.json to install Elasticsearch plugin.

And now run 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 Elasticsearch index and credential, We just connect Elasticsearch with laravel and get the data from there.You can getting started with laravel by following Laravel 7 Installation And Example.

The 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 following command –

php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider"

Now go to the config file at app >> config >> elasticsearch.php and add default index name –

'default_index' => 'employees'

How To Connect Elasticsearch with Laravel

The laravel application has a .env file that will use for configuration with Elasticsearch. Open .env file and updated below entries –

How To Create ES Client

We will use ClientBuilder method to create es client into the laravel, I also need Elasticsearch host to connect with them.

How To Fetch Data From the Elasticsearch

Let’s fetch data from elasticsearch using ESClient,We will create all() method that will takes $from and $size as a parameters and passed to elasticsearch.

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 7 and fetched all data.

If you have any questions regarding this laravel 7 article, or with the configuration of Elasticsearch in Laravel, feel free to mention them in the comment section below.