All Projects → pmatseykanets → Laravel Scout Postgres

pmatseykanets / Laravel Scout Postgres

Licence: mit
PostgreSQL Full Text Search Engine for Laravel Scout

Projects that are alternatives of or similar to Laravel Scout Postgres

Plv8
V8 Engine Javascript Procedural Language add-on for PostgreSQL
Stars: ✭ 1,195 (+753.57%)
Mutual labels:  database, postgresql, postgres
Lara Lens
Laravel package for display diagnostic (config, database, http connections...)
Stars: ✭ 96 (-31.43%)
Mutual labels:  database, laravel, laravel-package
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+805%)
Mutual labels:  database, postgresql, postgres
Postgresclientkit
A PostgreSQL client library for Swift. Does not require libpq.
Stars: ✭ 49 (-65%)
Mutual labels:  database, postgresql, postgres
Libpq.jl
A Julia wrapper for libpq
Stars: ✭ 109 (-22.14%)
Mutual labels:  database, postgresql, postgres
Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-59.29%)
Mutual labels:  database, postgresql, postgres
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+12877.14%)
Mutual labels:  database, postgresql, postgres
Laravel Pg Extensions
Laravel extensions for Postgres
Stars: ✭ 33 (-76.43%)
Mutual labels:  database, postgresql, laravel
Pg flame
A flamegraph generator for Postgres EXPLAIN ANALYZE output.
Stars: ✭ 1,391 (+893.57%)
Mutual labels:  database, postgresql, postgres
Activerecord Clean Db Structure
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Stars: ✭ 101 (-27.86%)
Mutual labels:  database, postgresql, postgres
Laravel Translatable
It's a Laravel database translations manager
Stars: ✭ 47 (-66.43%)
Mutual labels:  database, laravel, laravel-package
Backup Manager
Database backup manager for dumping to and restoring databases from S3, Dropbox, FTP, SFTP, and Rackspace Cloud
Stars: ✭ 1,589 (+1035%)
Mutual labels:  database, postgresql, laravel
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (-72.14%)
Mutual labels:  database, postgresql, postgres
Backup
MySQL Database backup package for Laravel
Stars: ✭ 66 (-52.86%)
Mutual labels:  database, laravel, laravel-package
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+602.86%)
Mutual labels:  database, postgresql, postgres
Laravel Sync Migration
Developer tool helps to sync migrations without refreshing the database
Stars: ✭ 89 (-36.43%)
Mutual labels:  database, laravel, laravel-package
Awesome Postgres
A curated list of awesome PostgreSQL software, libraries, tools and resources, inspired by awesome-mysql
Stars: ✭ 7,468 (+5234.29%)
Mutual labels:  database, postgresql, postgres
Laravel Database Logger
Log database query sql in Laravel Application, support Guard,Auth to multiple file record
Stars: ✭ 31 (-77.86%)
Mutual labels:  database, laravel, laravel-package
Pgcli
Postgres CLI with autocompletion and syntax highlighting
Stars: ✭ 9,985 (+7032.14%)
Mutual labels:  database, postgresql, postgres
Ship Hold
data access framework for Postgresql on nodejs
Stars: ✭ 110 (-21.43%)
Mutual labels:  database, postgresql, postgres

PostgreSQL Full Text Search Engine for Laravel Scout

Latest Version on Packagist Software License tests StyleCI Total Downloads License

This package makes it easy to use native PostgreSQL Full Text Search capabilities with Laravel Scout.

If you find this package usefull, please consider bying me a coffee.

Buy Me a Coffee at ko-fi.com

Contents

Installation

You can install the package via composer:

composer require pmatseykanets/laravel-scout-postgres

Laravel

If you're using Laravel < 5.5 or if you have package auto-discovery turned off you have to manually register the service provider:

// config/app.php
'providers' => [
    ...
    ScoutEngines\Postgres\PostgresEngineServiceProvider::class,
],

Lumen

Scout service provider uses config_path helper that is not included in Lumen. To fix this include the following snippet either directly in bootstrap.app or in your autoloaded helpers file i.e. app/helpers.php.

if (! function_exists('config_path')) {
    /**
     * Get the configuration path.
     *
     * @param  string  $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->basePath() . '/config'.($path ? DIRECTORY_SEPARATOR.$path : $path);
    }
}

Create the scout.php config file in app/config folder with the following contents

<?php

return [
    'driver' => env('SCOUT_DRIVER', 'pgsql'),
    'prefix' => env('SCOUT_PREFIX', ''),
    'queue' => false,
    'pgsql' => [
        'connection' => 'pgsql',
        'maintain_index' => true,
        'config' => 'english',
    ],
];

Register service providers:

// bootstrap/app.php
$app->register(Laravel\Scout\ScoutServiceProvider::class);
$app->configure('scout');
$app->register(ScoutEngines\Postgres\PostgresEngineServiceProvider::class);

Configuration

Configuring the Engine

Specify the database connection that should be used to access indexed documents in the Laravel Scout configuration file config/scout.php:

// config/scout.php
...
'pgsql' => [
    // Connection to use. See config/database.php
    'connection' => env('DB_CONNECTION', 'pgsql'),
    // You may want to update index documents directly in PostgreSQL (i.e. via triggers).
    // In this case you can set this value to false.
    'maintain_index' => true,
    // You can explicitly specify what PostgreSQL text search config to use by scout.
    // Use \dF in psql to see all available configurations in your database.
    'config' => 'english',
    // You may set the default querying method
    // Possible values: plainquery, phrasequery, tsquery
    // plainquery is used if this option is omitted.
    'search_using' => 'tsquery'
],
...

Configuring PostgreSQL

Make sure that an appropriate default text search configuration is set globbaly (in postgresql.conf), for a particular database (ALTER DATABASE ... SET default_text_search_config TO ...) or alternatively set default_text_search_config in each session.

To check the current value

SHOW default_text_search_config;

Prepare the Schema

By default the engine expects that parsed documents (model data) are stored in the same table as the Model in a column searchable of type tsvector. You'd need to create this column and an index in your schema. You can choose between GIN and GiST indexes in PostgreSQL.

class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->text('title');
            $table->text('content')->nullable();
            $table->integer('user_id');
            $table->timestamps();
        });

        DB::statement('ALTER TABLE posts ADD searchable tsvector NULL');
        DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)');
        // Or alternatively
        // DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)');
    }

    public function down()
    {
        Schema::drop('posts');
    }
}

Configuring Searchable Data

In addition to Model's attributes you can bring other any other data to the index document. I.e. a list of Tags for a Post.

public function toSearchableArray()
{
    return [
        'title' => $this->title,
        'content' => $this->content,
        'author' => $this->user->name,
        'tags' => $this->tags->pluck('tag')->implode(' '),
    ];
}

Configuring the Model

You may fine tune the engine behavior for a particular Model by implemeting searchableOptions() in your Model.

class Post extends Model
{
    use Searchable;

    // ...
    public function searchableOptions()
    {
        return [
            // You may wish to change the default name of the column
            // that holds parsed documents
            'column' => 'indexable',
            // You may want to store the index outside of the Model table
            // In that case let the engine know by setting this parameter to true.
            'external' => true,
            // If you don't want scout to maintain the index for you
            // You can turn it off either for a Model or globally
            'maintain_index' => true,
            // Ranking groups that will be assigned to fields
            // when document is being parsed.
            // Available groups: A, B, C and D.
            'rank' => [
                'fields' => [
                    'title' => 'A',
                    'content' => 'B',
                    'author' => 'D',
                    'tags' => 'C',
                ],
                // Ranking weights for searches.
                // [D-weight, C-weight, B-weight, A-weight].
                // Default [0.1, 0.2, 0.4, 1.0].
                'weights' => [0.1, 0.2, 0.4, 1.0],
                // Ranking function [ts_rank | ts_rank_cd]. Default ts_rank.
                'function' => 'ts_rank_cd',
                // Normalization index. Default 0.
                'normalization' => 32,
            ],
            // You can explicitly specify a PostgreSQL text search configuration for the model.
            // Use \dF in psql to see all available configurationsin your database.
            'config' => 'simple',
        ];
    }
}
...

If you decide to keep your Model's index outside of the Model's table you can let engine know that you want to push additional fields in the index table that you can then use to filter the result set by applying where() with the Scout Builder. In this case you'd need to implement searchableAdditionalArray() on your Model. Of course the schema for the external table should include these additional columns.

public function searchableAdditionalArray()
{
    return [
        'user_id' => $this->user_id,
    ];
}

You may want to make your searchable column hidden so it's not standing in your way

protected $hidden = [
    'searchable',
];

Usage

// plainto_tsquery()
$posts = App\Post::search('cat rat')
    ->usingPlainQuery()->get()

// phraseto_tsquery()
$posts = App\Post::search('cat rat')
    ->usingPhraseQuery()->get()

// to_tsquery()
$posts = App\Post::search('fat & (cat | rat)')
    ->usingTsQuery()->get()

// websearch_to_tsquery()
// uses web search syntax 
$posts = App\Post::search('"sad cat" or "fat rat" -mouse')
    ->usingWebSearchQuery()->get()

// DIY using a callback
use ScoutEngines\Postgres\TsQuery\ToTsQuery;

$results = App\Post::search('fat & (cat | rat)', function ($builder, $config) {
    return new ToTsQuery($builder->query, $config);
})->get();

Please see the official documentation on how to use Laravel Scout.

Testing

composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].