DEV Community

Cover image for How to setup Meilisearch and Laravel Scout
Adam Miedema
Adam Miedema

Posted on • Originally published at cleavr.io

How to setup Meilisearch and Laravel Scout


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

  return [
'id' => $array['id'],
'title' => $array['title'],
'body'=> $array['body']
   ];
}
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Step 4: Test out search

>>> 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 …",
       },
     ],
   }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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@@
Enter fullscreen mode Exit fullscreen mode

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 documentaion for more info on how to use Meilisearch with your project.

Top comments (0)