OneBite.Dev - Coding blog in a bite size

How to add meilisearch in Laravel

Learn how to install meilisearch in your Laravel app. Make your search system smarter

Make your Laravel app’s search system smarter by adding Meilisearch.

What is meilisearch?

Meilisearch is an open source search-engine that we can self-host. Think of it as Algolia, but free :P.

Step by step

We will use Laravel Scout package to provide full-text search for our Eloquent models in Laravel project.

Install scout via composer

composer require laravel/scout

Publish scout config

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

Add searchable to model

Add searchable trait to the model(s), you want the full-text search capability in. For example you have a Model Post.

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;  // add this
 
class Post extends Model
{
    use Searchable; // add this
}

Choose your drive

You can use Algolia, or in this case we will use Meilisearch. So install dependencies with:

composer require meilisearch/meilisearch-php http-interop/http-factory-guzzle

Take notes of your masterKey, copy and paste it in a safe place.

Update .env

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=masterKey

Install meilisearch

To install meilisearch, you can follow this guide: how to install meilisearch in Ubuntu. It works for production as well. It’s tested in Ubuntu droplet in DigitalOcean.

Optional :adding queue

From laravel docs

While not strictly required to use Scout, you should strongly consider configuring a queue driver before using the library. Running a queue worker will allow Scout to queue all operations that sync your model information to your search indexes, providing much better response times for your application’s web interface.

Update your config/scout.php to

'queue' => true,

To specify the connection, in queue config add:

'queue' => [
    'connection' => 'redis',
    'queue' => 'scout'
],
search