toanld/laravel-sphinx

Laravel 的 SphinxQL 查询构建器

安装: 24

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 22

类型:项目

v1.0.3 2023-10-14 07:30 UTC

This package is auto-updated.

Last update: 2024-09-14 09:33:21 UTC


README

Build Status Latest Stable Version

Laravel-Sphinx 数据库连接器,提供具有表现力的查询构建器、Eloquent、ActiveRecord 风格的 ORM

安装

laravel-sphinx 可以通过将 Composer 添加为项目 composer.json 文件中的依赖项来安装。

{
    "require": {
        "toanld/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 = 1id = '1' 并不相同

  • integer 用于整数类型 attr_uint

  • float 用于浮点类型 attr_float

  • bool (integer) 用于布尔类型 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,也可以是 ASCDESC,不区分大小写。

  • $sq->orderBy($column, $direction = null)

    ORDER BY $column [$direction]

    方向可以省略为 null,也可以是 ASCDESC,不区分大小写。

  • $sq->option($name, $value)

    OPTION $name = $value

    为查询设置 SphinxQL 选项,如 max_matchesreverse_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

    对于 innot 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

资源