<-- Back to The Cleavr Slice

28 February 2023

blog

tips

How to setup Meilisearch and Laravel Scout

Meilisearch is an open-source search system for your websites and apps that provides relevant search results, quickly.

Laravel Scout is like a superhero of search packages! It has the power to make Eloquent models searchable with full-text search capabilities. It's like a magic wand that creates search indexes for each record stored in the database, so searching is fast and efficient.

If you're ready to take your Laravel site to the next level, then this tutorial is for you!

We'll take you through the steps of adding Laravel Scout and Meilisearch to your project. After that, you'll be able to search your site with ease. So what are you waiting for? Let's get started!

Step 1: Install Laravel Scout.

Let's get this party started! Let's fire up our terminal, navigate to your project's root directory, and get the main laravel scout dependency installed.

composer require laravel/scout

Scout will provide an assets library, let's get them published! You can do this by running the following command.

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Step 2: Set Up models for Scout

Adding the Laravel\Scout\Searchable trait to the Post model is the first step to making it searchable. After that, Laravel will take care of the rest! Whenever a Post record is added, updated, or deleted, it will be automatically synchronized with the Meilisearch index - making it easy to find the posts you're looking for.

Keep in mind, our example uses a Post.php model. You'll ultimately add this to whichever model you are looking to index for search results.

The /app/Models/Post.php file should look like this:

<?php
namespace App;

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

class Post extends Model
{
    use Searchable;

    ...
    ...
}

Now, with our entity integrated with Laravel Scout, we can be sure that only the properties we want are included in the index. This makes the index much more lightweight and efficient. To make sure of this, we need to override the toSearchableArray function so that only the desired properties are indexed.

Continuing in the /app/Models/Post.php file, add the following:

public function toSearchableArray()
{
  $array = $this->toArray();

  return [
'id' => $array['id'],
'title' => $array['title'],
'body'=> $array['body']
   ];
}

Step 3: Import existing database records to search indexes

Laravel Scout is like a magical genie, providing us with a simple way to import records. With just one artisan command, you can quickly and easily import data into your database. Let's take a look at an example to see just how easy it is!

❯ php artisan scout:import "App\Models\Post"
Imported [App\Models\Post] models up to ID: 10
All [App\Models\Post] records have been imported.
>>> App\Models\Post::search('Lorem')->get();
=> Illuminate\Database\Eloquent\Collection {#4219
     all: [
       App\Models\User {#4230
         id: 5,
         title: "Lorem ipsum dolor sit amet, consectetur",
         body: "...Sed ut perspiciatis unde omnis iste natus …",
       },
     ],
   }

Step 5: Install Meilisearch on your server

Install Meilisearch on your server following the official documentation. Use the Quick Script feature in Cleavr to add the following script and then run it on the target server to install Meilisearch.

# Add Meilisearch package
echo "deb [trusted=yes] https://apt.fury.io/meilisearch/ /" | sudo tee /etc/apt/sources.list.d/fury.list

# Update APT and install Meilisearch
sudo apt update && sudo apt install meilisearch

# Launch Meilisearch
meilisearch

Step 6: Add a process monitor for Meilisearch

Now, let's add a process monitor in the Server > Process Monitors section. With a process monitor, you can start Meilisearch and keep it running smoothly.

add a new process monitor for meilisearch

Update the master key test123@@ with the master key you'd like to use. Keep note of your master key as you'll need to add it in the next section to your environment file.

Step 7: Update your project's environment file

Ready, set, update! It's time to make sure your project environment variables are up to date.

Head over to Deployment > Workflow > Environment and get to work. Don't forget to add the following variables and you'll be good to go.

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
SCOUT_QUEUE=true
MEILISEARCH_KEY=test123@@

Voila! You now have Meilisearch set up and ready to serve up relevant search results in no time. Now you can impress your friends and family with your lightning-fast search capabilities!

Check out Meilisearch documentation for more info on how to use Meilisearch with your project.

Take control of your servers and deployments.Without breaking a sweat.

Sign up for a 5-day free trial of Cleavr Pro. No credit card required until you decide to subscribe.

Sign up for free