Laravel Scout is one of the most commonly used packages in Laravel for adding full-text search to the Eloquent Model. Laravel Scout Elasticsearch provides a simple, driver-based solution for adding full-text search to your Eloquent models. It makes it easy to index and search the contents of the Eloquent model via continuous syncing. In addition, it keeps your search indexes in regular sync using model observers.
By default, Laravel Scout comes pre-configured for Algolia – a blazing fast Laravel autocomplete search service. But for the purpose of this article, we will configure it with Laravel Scout Elasticsearch, which is also one of the finest web tools available for managing search indexes in applications.
Setup Laravel Scout Elasticsearch
Let’s begin by installing Scout in our project by running the following command:
composer require laravel/scout
Once you have installed it, you can publish its config file by running the following command:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Now, let’s install Elasticsearch package for the PHP and Elasticsearch Eloquent package for Laravel by running the following commands:
composer require elasticsearch/elasticsearch composer require tamayo/laravel-scout-elastic
Now, open your config/app.php file and add the providers in it as shown below:
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
All the required packages are now successfully installed. It’s time to configure app/scout.php to use the Elasticsearch driver.
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
Add the configuration for Elasticsearch in the same file as given below:
'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'blog'), 'config' => [ 'hosts' => [ env('ELASTICSEARCH_HOST', 'localhost'), ], ], ],
Once done, the next step is to make a model which will be searchable by Elasticsearch for full-text.
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
Creating Model For Full-Text Search
Run the following command to create a model:
php artisan make:model Blog
Mentioned below is the table definition of blogs, which I have created using Cloudways Database Manager:
Now, add the Laravel Scout Searchable trait in the newly generated model. Here’s how to do it:
The next step is to use Laravel Tinker to add some testing blogs in our table containing title and content.
Now, import this model in Scout to make it searchable with Elasticsearch.
php artisan scout:import “App\Blog”
Testing Full-Text Search
We have added data in the blogs. We will test the full-text search using Tinker. Run the following commands to search once Tinker starts working:
App\Blog::search('scout')->get(); App\Blog::search('full text')->get();
Check out the result of both commands in the image given below:
Conclusion
This article is your detailed guide through the process of configuring Elasticsearch with Laravel Scout. It is a great driver-based tool which helps adding full-text search and fast indexing in the Eloquent model. Moreover, its integration in Laravel is quite easy as shown in the above demonstration.
If you have any questions regarding this article, or the configuration of Elasticsearch in Laravel, feel free to mention them in the comments section below.
Shahzeb Ahmed
Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]