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

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.

  • Laravel
    How to Register & Use Laravel Service Providers
    Sajal Soni

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 LaravelScoutSearchable 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.

Powered by WPeMatico

Leave a Comment

Scroll to Top