clydescobidal/larasearch

A Laravel package that provides FULLTEXT index search functionality

1.0.6 2023-12-07 14:57 UTC

This package is auto-updated.

Last update: 2024-06-07 16:19:52 UTC


README

bannercard.webp

Larasearch

Latest Stable Version Total Downloads License GitHub Actions

The goal of this Laravel package is to offer fast FULLTEXT indexed searches. This is only relevant if you wish to include a basic search feature in your project. However, if your project has a large amount of data that needs to be searched and is frequently used, search engines like Typesense, ElasticSearch, Algolia, and similar ones are more appropriate.

Search queries are executed on the searchable table to save your main table from the heavy search workload.

Features

  • FULLTEXT index search
  • Cached results

Installation

You can install the package via composer:

composer require clydescobidal/larasearch

Publish the configuration file:

php artisan vendor:publish --provider="Clydescobidal\Larasearch\LarasearchServiceProvider"

Run migration:

php artisan migrate

Usage

Add the Clydescobidal\Larasearch\Searchable trait to the model you would like to make searchable. Models that are using this trait will be indexed in the searchable table whenever changes are made on the model.

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Clydescobidal\Larasearch\Searchable;
 
class Post extends Model
{
    use Searchable;
}

Search for posts and chain query builder methods as normal:

<?php

$post= Post::search('my post title')->get();
$posts = Post::search('search posts')->paginate();

Make model searchable or unsearchable:

<?php

$post= Post::find(1);
$post->searchable(); // Adds this model to the search index

$post= Post::find(2);
$post->unsearchable(); // Removes this model from the search index

Commands

You can run the command below if you want to make all your models searchable. Note that this will only work on models with Clydescobidal\Larasearch\Searchable trait. This is applicable when you first install the package and you want your existing models to be searchable, or when you want to do a batch reindex of a model.

In this example, we will make all instances of App\Models\Post searchable.

php artisan make:searchable "App\Models\Post"

We can also do a batch unsearchable on a model.

php artisan make:unsearchable "App\Models\Post"

Config

Property Type Default Description
table string searchable The table where the searchable indices are stored
cache boolean true Enable caching of results
queue boolean true Run searchable syncs in queue (recommended)

Cache

By default, search query results are cached. You can turn this off by setting the cache property in the configuration. In any case you want to clear the cached results, you can run the artisan command:

php artisan cache:clear --tags="App\Models\Post"

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.