diezeel/laravel-sphinx

A Laravel query builder for SphinxQL

Installs: 4 855

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 13

Type:project

v0.5.0 2019-11-02 23:20 UTC

This package is auto-updated.

Last update: 2020-09-03 01:14:06 UTC


README

Build Status Latest Stable Version

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": {
        "diezeel/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

DieZeeL\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,
    ],

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 \DieZeeL\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 \DieZeeL\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 and id = '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 be ASC or DESC case insensitive.

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

    ORDER BY $column [$direction]

    Direction can be omitted with null, or be ASC or DESC case insensitive.

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

    OPTION $name = $value

    Set a SphinxQL option such as max_matches or reverse_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 and not 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

Resources