This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

Configure Elasticsearch With Laravel Scout in Few Easy Steps

Updated on September 23, 2021

3 Min Read
laravel elasticsearch scout

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:

create full text search table model

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.

laravel tinker

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:

laravel tinker result

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

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]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now