Skip to content

Laravel Support

Laravel-RediSearch allows for indexing and searching Laravel models. It provides a Laravel Scout driver.

Terminal window
composer require ethanhann/laravel-redisearch

Add this entry to the providers array in config/app.php.

Ehann\LaravelRediSearch\RediSearchServiceProvider::class

Update the Scout driver in config/scout.php.

'driver' => env('SCOUT_DRIVER', 'ehann-redisearch'),

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 “Product” model that is configured to be searchable:

Terminal window
artisan ehann:redisearch:import App\\Product

Delete the index before importing:

Terminal window
artisan ehann:redisearch:import App\\Product --recreate-index

Import models without an ID field (this should be rarely needed):

Terminal window
artisan ehann:redisearch:import App\\Product --no-id

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()

See the Laravel Scout documentation for additional information.