constantable/laravel-scout-sphinx

Laravel Scout Sphinx Driver

3.0 2023-09-11 21:11 UTC

This package is auto-updated.

Last update: 2024-12-12 00:02:51 UTC


README

Introduction

This package is fork of constantable/laravel-scout-sphinx. package offers advanced functionality for searching and filtering data using the Sphinx full text search server for Laravel Scout.

Installation

Composer

Use the following command to install this package via Composer.

composer require constantable/laravel-scout-sphinx

Configuration

Publish the Scout configuration using the vendor:publish Artisan command.

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

This command will publish the scout.php configuration file to your config directory, which you can than edit and set sphinxsearch as the Scout driver.

'driver' => env('SCOUT_DRIVER', 'sphinxsearch'),

To configure the connection to Sphinx server add the following (i.e. default) connection options.

    'sphinxsearch' => [
        'host' => env('SPHINX_HOST', 'localhost'),
        'port' => env('SPHINX_PORT', '9306'),
        'socket' => env('SPHINX_SOCKET'),
        'charset' => env('SPHINX_CHARSET'),
    ],

Override these variables in your .env file if required.

Usage

  • Add the Laravel\Scout\Searchable trait to the model you would like to make searchable.
  • Customize the index name and searchable data for the model:
    public function searchableAs()
    {
        return 'posts_index';
    }
    
    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }

A basic search:

$orders = App\Order::search('Star Trek')->get();

Please refer to the Scout documentation for additional information. You can run more complex queries on the index by using a callback, setting the where clause, orderBy, or paginate threshold. For example:

$orders = App\Order::search($keyword, function (SphinxQL $query) {
        return $query->groupBy('description');
    })            
    ->where('status', 1)
    ->orderBy('date', 'DESC')
    ->paginate(20);

Note: Changes on Sphinx indexes are only allowed for RT (Real-time) indexes. If you have these and need to update/delete records please define public $isRT = true; in the model's property.

Credits

License

Licensed under the MIT license