Laravel Support
Laravel-RediSearch allows for indexing and searching Laravel models. It provides a Laravel Scout driver.
Getting Started
Section titled “Getting Started”Install
Section titled “Install”composer require ethanhann/laravel-redisearchRegister the Provider
Section titled “Register the Provider”Add this entry to the providers array in config/app.php.
Ehann\LaravelRediSearch\RediSearchServiceProvider::classConfigure the Scout Driver
Section titled “Configure the Scout Driver”Update the Scout driver in config/scout.php.
'driver' => env('SCOUT_DRIVER', 'ehann-redisearch'),Define Searchable Schema
Section titled “Define Searchable Schema”Define the field types that will be used on indexing
<?php
namespace App;
use Laravel\Scout\Searchable;...use Ehann\RediSearch\Fields\TextField;use Ehann\RediSearch\Fields\GeoField;use Ehann\RediSearch\Fields\NumericField;use Ehann\RediSearch\Fields\TagField;use Ehann\RediSearch\Fields\GeoLocation;...
class User extends Model { use Searchable;
public function searchableAs() { return "user_index"; }
public function toSearchableArray() { return [ "name" => $this->name, "username" => $this->username, "location" => new GeoLocation( $this->longitude, $this->latitude ) "age" => $this->age, ]; }
public function searchableSchema() { return [ "name" => TextField::class, "username" => TextField::class, "location" => GeoField::class, "age" => NumericField::class ]; }}Import a Model
Section titled “Import a Model”Import a “Product” model that is configured to be searchable:
artisan ehann:redisearch:import App\\ProductDelete the index before importing:
artisan ehann:redisearch:import App\\Product --recreate-indexImport models without an ID field (this should be rarely needed):
artisan ehann:redisearch:import App\\Product --no-idQuery Filters
Section titled “Query Filters”How To Query Filters? Filtering Tag Fields
App\User::search("Search Query", function($index){ return $filter->geoFilter("location", 5.56475, 5.75516, 100) ->numericFilter('age', 18, 32)})->get()What now?
Section titled “What now?”See the Laravel Scout documentation for additional information.