Laravel Meilisearch Integration

In this post, you’ll learn how to integrate meilisearch with laravel

Laravel Meilisearch Integration

MeiliSearch is an open source search-engine that will improve search experience.

Install MeiliSearch cURL

curl -L https://install.meilisearch.com | sh

./meilisearch

MeiliSearch Dashboard

MeiliSearch Doc

https://docs.meilisearch.com/learn/getting_started/quick_start.html

Install Laravel Scout

install Laravel Scout using composer

composer require laravel/scout

After installing Scout, you should publish the Scout configuration file using the vendor:publish Artisan command.

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
 
class Product extends Model
{
    use Searchable;
}

Install MeiliSearch PHP Driver

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

.env Configuration File

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

Add Searchable Trait to Your Model

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

Add Searchable Index name to Your Model

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
 
class Product extends Model
{
    use Searchable;

    /**
     * Get the name of the index associated with the model.
     *
     * @return string
     */
    public function searchableAs()
    {
        return 'products_index';
    }
}

Import Data to MeiliSearch

If you are installing Scout into an existing project, you may already have database records you need to import into your indexes

php artisan scout:import "App\Models\Product"

MeiliSearch Dashboard & Index Data

MeiliSearch imported data, just go localhost with port 7700.

http://127.0.0.1:7700

Search Model

    public function __invoke(Request $request)
    {
        $query   = $request->get('query');
        $results = Product::search($query)->paginate(20);
        return view('product', compact('results'));
    }

Now you should be able to set up and implement MeiliSearch with Laravel. If you this tutorial to be helpful, do share.

About Tech Flow Zone

Check Also

Laravel Query Optimization

Laravel Query Optimization

A well-liked PHP framework for creating web apps is called Laravel. Laravel’s robust query builder, …

Leave a Reply

Your email address will not be published. Required fields are marked *