serdartaylan / laravel-sphinx
用于 SphinxQL 的 Laravel 查询构建器
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
Suggests
- laravel/framework: The Laravel Framework
- laravel/scout: The Laravel scout
README
分支
Laravel-Sphinx 数据库连接器,提供表达式查询构建器、Eloquent 和 ActiveRecord 风格的 ORM
安装
laravel-sphinx 可以通过将 Composer 添加为项目 composer.json 文件中的依赖项来安装。
{ "require": { "serdartaylan/laravel-sphinx": "*" } }
有关更详细的安装和用法说明,请参阅 Composer 的文档。
配置
更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组中
Fobia\Database\SphinxConnection\SphinxServiceProvider::class,
最后,您可以将 Sphinx 连接
添加到 config/database.php 中的数据库数组中
'sphinx' => [ 'driver' => 'sphinx', 'host' => env('SPHINX_HOST', env('DB_HOST','127.0.0.1')), 'port' => 9306, 'database' => '', ],
用法
获取连接并构建查询
$db = \DB::connection('sphinx');
使用查询构建器
$users = $db->table('rt')->where('votes', '>', 100)->get();
使用 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();
属性转换 对于 attr_multi
列的结果,可以选择格式,将其转换为数组。
对于类型为 attr_multi
的列,'(1, 2, 3)'
的值转换为数组 [1, 2, 3]
class Product extends \Fobia\Database\SphinxConnection\Eloquent\Model { protected $casts = [ 'tags' => 'mva', ]; }
查询构建器
$sq = $db->table('rt');
构建查询时,使用值的强类型(如何在 SphinxQl 中)。
注意:
id = 1
和id = '1'
不相同
-
integer 用于类型化整数
attr_uint
-
float 用于类型化浮点数
attr_float
-
bool (整数) 用于类型化布尔值
attr_bool
,将被转换为整数(0 或 1) -
array (MVA) 用于类型化 MVA
attr_multi
$sq->insert([ 'id' => 1, 'tags' => [1, 2, 3] ]); // Output: INSERT INTO rt (id, tags) VALUES(1, (1, 2, 3))
-
string - 字符串值,当请求时被转义
$sq->insert([ 'id' => 1, 'name' => "name 'text'" ]); // Output: INSERT INTO rt (id, name) VALUES(1, 'name \'text\'')
MATCH
-
$sq->match($column, $value, $half = false)
在全文字段中搜索。可以在同一查询中使用多次。列可以是数组。值可以是表达式以绕过转义(并使用您自己的自定义解决方案)。
<?php $sq->match('title', 'Otoshimono') ->match('character', 'Nymph') ->match(array('hates', 'despises'), 'Oregano'); $sq->match(function(\Foolz\SphinxQL\Match $m) { $m->not('text'); });
对于
match
函数使用的库 SphinxQL::match
WITHIN GROUP, ORDER, OPTION
-
$sq->withinGroupOrderBy($column, $direction = 'ASC')
WITHIN GROUP ORDER BY $column [$direction]
方向可以省略为
null
,或为ASC
或DESC
(不区分大小写)。 -
$sq->orderBy($column, $direction = null)
ORDER BY $column [$direction]
方向可以省略为
null
,或为ASC
或DESC
(不区分大小写)。 -
$sq->option($name, $value)
OPTION $name = $value
为查询设置 SphinxQL 选项,例如
max_matches
或reverse_scan
。
whereMulti
-
$sq->whereMulti($column, $operator, $values)
所有参数转换为平面数组
$sq->whereMulti('id', '=', [1, 2, 3, [4, 5]]); // Output: WHERE id = 1 and id = 2 and id = 3 and id = 4 and id = 5
对于
in
和not in
是不同的$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)
替换
-
$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()