ElasticSearch Example with Laravel 9

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'

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 –

ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=
ELASTICSEARCH_PASS=

How To Create ES Client

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();    
}

How To Fetch Data From the Elasticsearch

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.

Conclusion

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.

Leave a Reply

Your email address will not be published.