Advertisement
  1. Code
  2. Laravel 5
Code

How to Set Up a Full-Text Search Using Scout in Laravel

by
Difficulty:IntermediateLength:MediumLanguages:

Full-text search is crucial for allowing users to navigate content-rich websites. In this post, I'll show you how to implement full-text search for a Laravel app. In fact, we'll use the Laravel Scout library, which makes implementation of full-text search easy and fun.

What exactly is the Laravel Scout? The official documentation sums it up like this:

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.

Basically, Laravel Scout is a library that manages manipulation of the index whenever there's a change in the model data. The place where the data will be indexed depends on the driver that you've configured with the Scout library.

As of now, the Scout library supports Algolia, a cloud-based search engine API, and that's what we'll use in this article to demonstrate the full-text search implementation.

We'll start by installing the Scout and Algolia server libraries, and as we move on we'll go through a real-world example to demonstrate how you could index and search your data.

Server Configurations

In this section, we're going to install the dependencies that are required in order to make the Scout library work with Laravel. After installation, we'll need to go through quite a bit of configuration so that Laravel can detect the Scout library.

Let's go ahead and install the Scout library using Composer.

That's pretty much it as far as the Scout library installation is concerned. Now that we've installed the Scout library, let's make sure that Laravel knows about it.

Working with Laravel, you're probably aware of the concept of a service provider, which allows you to configure services in your application. Thus, whenever you want to enable a new service in your Laravel application, you just need to add an associated service provider entry in the config/app.php.

If you're not familiar with Laravel service providers yet, I would strongly recommend that you do yourself a favor and go through this introductory article that explains the basics of service providers in Laravel.

In our case, we just need to add the ScoutServiceProvider provider to the list of service providers in config/app.php, as shown in the following snippet.

Now, Laravel is aware of the ScoutServiceProvider service provider. The Scout library comes with a configuration file that allows us to set API credentials.

Let's go ahead and publish the assets provided by the Scout library using the following command.

As you can see, it has copied the vendor/laravel/scout/config/scout.php file to config/scout.php.

Next, go ahead and create an account with Algolia as we'll need API credentials in the first place. Once you have the API information, let's go ahead and configure the necessary settings in the config/scout.php file, as shown in the following snippet.

Note that we've set the value of SCOUT_DRIVER to algolia driver. Thus, it's required that you configure the necessary settings for the Algolia driver at the end of the file. Basically, you just need to set the id and secret that you've got from the Algolia account.

As you can see, we're fetching values from environment variables. So let's make sure that we set the following variables in the .env file properly.

Finally, we need to install the Algolia PHP SDK, which will be used to interact with the Algolia using APIs. Let's install it using the composer as shown in the following snippet.

And with that, we've installed all the dependencies that are necessary in order to post and index data to the Algolia service.

Make Models Indexable and Searchable

In the previous section, we did all the hard work to set up the Scout and Algolia libraries so that we could index and search data using the Algolia search service.

In this section, we'll go through an example that demonstrates how you could index the existing data and retrieve search results from Algolia. I assume that you have a default Post model in your application that we'll use in our example.

The first thing that we'll need to do is to add the Laravel\Scout\Searchable trait to the Post model. That makes the Post model searchable; Laravel synchronizes post records with the Algolia index every time the post record is added, updated, or deleted.

With that, the Post model is search-friendly!

Next, we would like to configure the fields that should get indexed in the first place. Of course, you don't want to index all the fields of your model in Algolia to keep it effective and lightweight. In fact, more often than not, you won't need it.

You can add the toSearchableArray in the model class to configure the fields that'll be indexed.

Now, we're ready to import and index existing Post records into Algolia. In fact, the Scout library makes this easy by providing the following artisan command.

That should import all the records of the Post model in a single go! They are indexed as soon as they're imported, so we're ready to query records already. Go ahead and explore the Algolia dashboard to see the imported records and other utilities.

How It Works Altogether

In this section, we'll create an example that demonstrates how to perform search and CRUD operations that are synced in real time with the Algolia index.

Go ahead and create the app/Http/Controllers/SearchController.php file with the following contents.

Of course, we need to add the associated routes as well.

Let's go through the query method to see how to perform a search in Algolia.

Recall that we made the Post model searchable by adding the Searchable trait. Thus, the Post model can use the search method to retrieve records from the Algolia index. In the above example, we're trying to fetch records that match the title keyword.

Next, there's the add method that imitates the workflow of adding a new post record.

There's nothing fancy in the above code; it just creates a new post record using the Post model. But the Post model implements the Searchable trait, so Laravel does some extra work this time around by indexing the newly created record in Algolia. So as you can see, the indexing is done in real time.

Finally, there's the delete method. Let's go through it as well.

As you would have expected, the record is deleted right away from the Algolia index as soon as it's deleted from the database.

Basically, there's no extra effort required from your side if you want to make existing models searchable. Everything is handled by the Scout library using model observers.

And that also brings us to the end of this article!

Conclusion

Today, we discussed how you can implement full-text search in Laravel using the Laravel Scout library. In the process, we went through the necessary installations and a real-world example to demonstrate it.

Feel free to ask if you have any queries or doubts using the comment feed below!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.