fobia / laravel-sphinx
A Laravel query builder for SphinxQL
Installs: 109 857
Dependents: 0
Suggesters: 0
Security: 0
Stars: 31
Watchers: 4
Forks: 23
Type:project
Requires
- php: >=7.2
- foolz/sphinxql-query-builder: ^3.0
Requires (Dev)
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- laravel/scout: ^7.0|^8.0
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.5
- symfony/filesystem: ^4.0|^5.0|^6.0
Suggests
- laravel/framework: The Laravel Framework
- laravel/scout: The Laravel scout
README
Laravel-Sphinx Database connector, providing an expressive query builder, Eloquent, ActiveRecord style ORM
Installation
laravel-sphinx can be installed with Composer by adding it as a dependency to your project's composer.json file.
{ "require": { "fobia/laravel-sphinx": "*" } }
Please refer to Composer's documentation for more detailed installation and usage instructions.
Config
After updating composer, add the ServiceProvider to the providers array in config/app.php
Fobia\Database\SphinxConnection\SphinxServiceProvider::class,
Finally you can just add Sphinx Connection
to the database array in config/database.php
'sphinx' => [ 'driver' => 'sphinx', 'host' => env('SPHINX_HOST', env('DB_HOST','127.0.0.1')), 'port' => 9306, 'database' => '', ],
Usage
Get a connection and build queries
$db = \DB::connection('sphinx');
Using The Query Builder
$users = $db->table('rt')->where('votes', '>', 100)->get();
Using The Eloquent ORM
class Product extends \Fobia\Database\SphinxConnection\Eloquent\Model {} $product = Product::find(1); $products = Product::where('votes', '>', 1)->get(); $products = Product::match('name', 'match text')->get();
Attribute Casting
For the results of the column attr_multi
can choose the format, which is converted to an array.
The values of '(1, 2, 3)'
for column type attr_multi
converted to an array [1, 2, 3]
class Product extends \Fobia\Database\SphinxConnection\Eloquent\Model { protected $casts = [ 'tags' => 'mva', ]; }
Query Builder
$sq = $db->table('rt');
For the build a query, using strong typing of values (how in SphinxQl).
Notice:
id = 1
andid = '1'
not the same
-
integer It is used to type integer
attr_uint
-
float It is used to type float
attr_float
-
bool (integer) It is used to type bool
attr_bool
, will be converted to integer (0 or 1) -
array (MVA) It is used to type MVA
attr_multi
$sq->insert([ 'id' => 1, 'tags' => [1, 2, 3] ]); // Output: INSERT INTO rt (id, tags) VALUES(1, (1, 2, 3))
-
string - string values, escaped when requested
$sq->insert([ 'id' => 1, 'name' => "name 'text'" ]); // Output: INSERT INTO rt (id, name) VALUES(1, 'name \'text\'')
MATCH
-
$sq->match($column, $value, $half = false)
Search in full-text fields. Can be used multiple times in the same query. Column can be an array. Value can be an Expression to bypass escaping (and use your own custom solution).
<?php $sq->match('title', 'Otoshimono') ->match('character', 'Nymph') ->match(array('hates', 'despises'), 'Oregano'); $sq->match(function(\Foolz\SphinxQL\Match $m) { $m->not('text'); });
For a function
match
used library SphinxQL::match
WITHIN GROUP, ORDER, OPTION
-
$sq->withinGroupOrderBy($column, $direction = 'ASC')
WITHIN GROUP ORDER BY $column [$direction]
Direction can be omitted with
null
, or beASC
orDESC
case insensitive. -
$sq->orderBy($column, $direction = null)
ORDER BY $column [$direction]
Direction can be omitted with
null
, or beASC
orDESC
case insensitive. -
$sq->option($name, $value)
OPTION $name = $value
Set a SphinxQL option such as
max_matches
orreverse_scan
for the query.
whereMulti
-
$sq->whereMulti($column, $operator, $values)
All parameters converted to a flat array
$sq->whereMulti('id', '=', [1, 2, 3, [4, 5]]); // Output: WHERE id = 1 and id = 2 and id = 3 and id = 4 and id = 5
For the
in
andnot in
is different$sq->whereMulti('id', 'in', [1, 2, 3]); // Output: WHERE id in (1) and id in (2) and id in (3)
$sq->whereMulti('id', 'in', [1, [20, 21], [30, 31]]); // Output: WHERE id in (1) and id in (20, 21) and id in (30, 31) // Equivalently $sq->whereMulti('id', 'in', 1, [20, 21], [30, 31]); // Output: WHERE id in (1) and id in (20, 21) and id in (30, 31)
Replace
-
$sq->replace($values)
$sq->replace([ 'id' => 1, 'name' => 'text name' ]);
API SphinxConnection
- \Foolz\SphinxQL\Drivers\Pdo\Connection $db->getSphinxQLDriversConnection()
- \Foolz\SphinxQL\Helper $db->getSphinxQLHelper()
- \Foolz\SphinxQL\SphinxQL $db->createSphinxQL()