Chat with us, powered by LiveChat

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.

Configure Elasticsearch With Laravel Scout in Few Easy Steps

July 8, 2019

3 Min Read
laravel elasticsearch
Reading Time: 3 minutes

Laravel Scout is one of the most commonly used packages in Laravel for adding full-text search in Eloquent Model. It is a driver-based solution made precisely to work with Eloquent. It makes it easy to index and search the contents of Eloquent model via continuous syncing. It facilitates to keep 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 Elasticsearch, which is also one of the finest web tools available for managing search indexes in applications.

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 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,

Lightning Fast Laravel Hosting With 24×7 Tech Support

We Take Care of Your Hosting Hassles To Let You Focus More On The Business

All the required packages are now successfully installed. It’s time to configure app/scout.php to use 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.

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 Blog 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:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Blog extends Model
{
   use Searchable;
  
   public $fillable = ["title","content"];
}

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 brings us to the end of this article. It demonstrates in detail how to configure Elasticsearch with Laravel Scout. It is a great driver-based tool which helps adding full-text search and fast indexing in 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 with the configuration of Elasticsearch in Laravel, feel free to mention them in the comment section below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at shahroze.nawaz@cloudways.com

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

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!