12

Full-Text Search with MeiliSearch and Laravel Scout

 3 years ago
source link: https://tighten.co/blog/full-text-search-with-meilisearch-and-scout/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Full-Text Search with MeiliSearch and Laravel Scout

Jose Soto Lead Programmer

Feature image: Full-Text Search with MeiliSearch and Laravel Scout

It feels so long ago that Laravel Scout was introduced: a great tool that lets you easily implement full-text search on Eloquent models. At launch, Laravel Scout included an Algolia driver–a service that provides hosted full-text search on their own servers–and options to make custom drivers. As with many hosted SaaS offerings, Algolia takes away a lot of headaches for developers and allows most teams to deploy quicker.

There are times, however, that I want to play with full-text search without going through Algolia. I wanted the simplicity of Algolia that I could host on my own, but without the complexities that I found in other self-hosted tools like Elasticsearch. I found this tool in MeiliSearch.

MeiliSearch is a simple self-hosted full-text search solution that's easy to integrate into a Laravel application–their team even maintains a Laravel Scout driver!

I'd like to show you how easily you can set up a locally-hosted full-text search server and connect it to your Laravel application. In this example, I will use a fresh install of Laravel 8, and will configure the App\Models\User model to be searchable via MeiliSearch/Scout.

Install Laravel Scout

composer require laravel/scout

Publish Scout configuration

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

Add the Laravel\Scout\Searchable trait to your model

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
+   use Laravel\Scout\Searchable;

    class User extends Authenticatable
    {
-       use HasFactory, Notifiable;
+       use HasFactory, Notifiable, Searchable;

Install MeiliSearch Laravel Scout driver

composer require meilisearch/meilisearch-laravel-scout

Publish MeiliSearch configuration

php artisan vendor:publish --provider="Meilisearch\Scout\MeilisearchServiceProvider" --tag="config"

Update .env

If you do not have a key set, be sure to leave the empty MEILISEARCH_KEY= value in your .env file

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

Run MeiliSearch

To make implementation into your local development environment easier, Tighten has included MeiliSearch as a service in Takeout. I highly recommend (completely biased) installing Takeout and starting a MeiliSearch service. Otherwise, you can follow the installation instructions on the MeiliSearch Documentation.

If you decide to use Takeout, you can run MeiliSearch with:

takeout enable meilisearch

Create a search index

❯ php artisan scout:index users
Index "users" created.

Import documents to populate the index

❯ php artisan scout:import "App\Models\User"
Imported [App\Models\User] models up to ID: 10
All [App\Models\User] records have been imported.

Search your models!

❯ php artisan tinker

>>> App\Models\User::search('Rach')->get();
=> Illuminate\Database\Eloquent\Collection {#4219
     all: [
       App\Models\User {#4230
         id: 5,
         name: "Rachelle Anderson",
         email: "[email protected]",
         email_verified_at: "2020-10-20 15:13:09",
         created_at: "2020-10-20 15:13:09",
         updated_at: "2020-10-20 15:13:09",
       },
     ],
   }

Keep going

The MeiliSearch documentation is comprehensive and covers a lot more use cases than this post. I recommend reading the documentation if you want to harness the full power of this great tool.

Stay in touch

I hope you come to enjoy working with MeiliSearch as much as I have. If you have feedback or would just like to say hi, follow me @JoseCanHelp. 👋


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK