The magic of Laravel Scout (Example with Elastic Search driver)

Murad Shukurlu
3 min readFeb 24, 2023

Hello folks,

In this blog post, I am gonna give you some short information about Laravel Scout and how it works with Elastic Search drivers.

This tutorial is useful especially for Laravel developers who want to start Elastic Search with Laravel Scout and understand how Elastic Search work.

Before getting dipper I want to add some quick information about Elastic Search and Laravel Scout.

Question 1: What is Elastic Search?

Elastic Search is a distributed search and analytics engine built on Apache Lucene, It means you can build your own search engine, and make easier your search results, Elastic Search takes care of your searching and gives you a comfortable way for what you what to find out.

Let us take a quick example:

If you have a table with billion data or you have pdf-s or other materials it is hard to go over them and find what you look up, Elastic Search takes care of them, you need to parse them into Elastic Search and let it help you for your searches!.

For additional information, you can look at the official site of elastic search

There are some alternatives to Elastic Search for building your own search engines.

Algolia

MeiliSearch

Question 2: What is Laravel Scout?

As Laravel documentation said :

Laravel Scout provides a simple, driver based solution for adding full-text search to your Eloquent models. Using model observers, Scout will automatically keep your search indexes in sync with your Eloquent records.

Laravel Scout provides us with a solution for working with full-text search Engines . As We mentioned before, there are some alternatives to Elastic search and Laravel default provides us working with Algolia and MeilliSearch, For working on Elastic Search we need to install an elastic search driver for Laravel,

There are some numbers of drivers to use Elastic Search with Laravel but I prefer to use this pretty package: https://github.com/Jeroen-G/Explorer.

For installing this package just add this package to your dependency:

composer require jeroen-g/explorer

Then publish the config file

php artisan vendor:publish --tag=explorer.config

then change SCOUT_DRIVER into elastic adding this row to .env file

SCOUT_DRIVER=elastic

You can connect to Elastic Search with the config/explorer.php file

I assume you have installed Elastic Search on your local environment if not you can apply for official documentation.

Let us add an example:

Think we have already an Article model with data we want to sync the model into elastic search with Laravel Scout how achieve this?

Firstly we need to add tell this model is searchable for this we need to add Searchable

use Laravel\Scout\Searchable;

use Searchable;

then add your model into the indexes array on the config/explorer.php file

'indexes' => [
\App\Models\Article::class
],

That is it!

Your model has already been converted into searchable!

To sync all older data you need to run this command

php artisan scout:import “App\Models\Article”

If everything is good, your data from the database should sync into Elastic Search!

Question 3: In every time my data changes do I need to run this import command?

Surely No!, When you work with Eloquent Models Laravel Scout takes care of your data, When you deleted your items it removes them from the search engine when you add and edit it does too!

That is the magic of Laravel Scout

If Laravel Scout would not exist we needed to use Logtash to sync our data.

Question 4: How to search ?

You can search via this example :

Article::search("Test")->get()

If you need to filter additional columns you can go with

Article::search("Test")->filter(new Term('column_name', $value))->get();

That is the end of our posts. I hope it was helpful to you,

Please subscribe to me for further new posts.

Good luck!

--

--