pmatseykanets/laravel-scout-postgres
Composer 安装命令:
composer require pmatseykanets/laravel-scout-postgres
包简介
PostgreSQL Full Text Search Driver for Laravel Scout
README 文档
README
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.
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 pmatseykanets@gmail.com 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.
pmatseykanets/laravel-scout-postgres 适用场景与选型建议
pmatseykanets/laravel-scout-postgres 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 218.42k 次下载、GitHub Stars 达 164, 最近一次更新时间为 2016 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「postgresql」 「laravel」 「full text search」 「fts」 「laravel scout」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pmatseykanets/laravel-scout-postgres 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pmatseykanets/laravel-scout-postgres 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pmatseykanets/laravel-scout-postgres 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Sql dumps containing databases for the linna apps.
统计信息
- 总下载量: 218.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 165
- 点击次数: 32
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-02