constantable / laravel-scout-sphinx
Laravel Scout Sphinx Driver
Installs: 47 890
Dependents: 1
Suggesters: 0
Security: 0
Stars: 20
Watchers: 2
Forks: 25
Open Issues: 3
Requires
- php: >=7.0
- foolz/sphinxql-query-builder: ^3.0
- laravel/scout: >=5.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.5|^8|^9
- roave/security-advisories: dev-latest
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