このパッケージにより、Laravel Scoutを使用して、ネイティブPostgreSQLフルテキスト検索機能を簡単に使用できます。
このパッケージが役立つ場合は、コーヒーをご覧ください。
Composer経由でパッケージをインストールできます。
composer require pmatseykanets/laravel-scout-postgresLaravel <5.5を使用している場合、またはパッケージの自動発見がオフになっている場合は、サービスプロバイダーを手動で登録する必要があります。
// config/app.php
' providers ' => [
...
ScoutEngines Postgres PostgresEngineServiceProvider::class,
],スカウトサービスプロバイダーは、ルーメンに含まれていないconfig_pathヘルパーを使用します。これを修正するには、次のスニペットがbootstrap.appまたは自動装置ヘルパーをFILE IE 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 );
}
}次のコンテンツを持つapp/configフォルダーにscout.php configファイルを作成します
<?php
return [
' driver ' => env ( ' SCOUT_DRIVER ' , ' pgsql ' ),
' prefix ' => env ( ' SCOUT_PREFIX ' , '' ),
' queue ' => false ,
' pgsql ' => [
' connection ' => ' pgsql ' ,
' maintain_index ' => true ,
' config ' => ' english ' ,
],
];登録サービスプロバイダー:
// bootstrap/app.php
$ app -> register ( Laravel Scout ScoutServiceProvider::class);
$ app -> configure ( ' scout ' );
$ app -> register ( ScoutEngines Postgres PostgresEngineServiceProvider::class);Laravel Scout Configurationファイル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 '
],
...適切なデフォルトのテキスト検索構成がGlobbaly( postgresql.confで)、特定のデータベース( ALTER DATABASE ... SET default_text_search_config TO ... )の場合、または各セッションでdefault_text_search_configを設定していることを確認してください。
現在の値を確認します
SHOW default_text_search_config;デフォルトでは、エンジンは、 tsvectorのタイプのsearchable列のモデルと同じ表に解析されたドキュメント(モデルデータ)が保存されることを期待しています。この列とスキーマにインデックスを作成する必要があります。 PostgreSQLのGINインデックスとGiSTインデックスを選択できます。
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 ' );
}
}Modelの属性に加えて、他の任意のデータをインデックスドキュメントに持ち込むことができます。つまり、投稿のタグのリスト。
public function toSearchableArray ()
{
return [
' title ' => $ this -> title ,
' content ' => $ this -> content ,
' author ' => $ this -> user -> name ,
' tags ' => $ this -> tags -> pluck ( ' tag ' )-> implode ( ' ' ),
];
}モデルにsearchableOptions()を実装することにより、特定のモデルのエンジンの動作を微調整できます。
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 ' ,
];
}
}
. . .モデルのインデックスをモデルのテーブルの外に置いておくことにした場合、エンジンにインデックステーブルの追加フィールドをプッシュすることを知らせることができます。その後、スカウトBuilderにwhere()を適用することで結果をフィルタリングします。この場合、モデルにsearchableAdditionalArray()を実装する必要があります。もちろん、外部テーブルのスキーマには、これらの追加列を含める必要があります。
public function searchableAdditionalArray ()
{
return [
' user_id ' => $ this -> user_id ,
];
}検索可能な列を隠してしまい、邪魔をしないようにすることができます
protected $ hidden = [
' searchable ' ,
]; // 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 ();Laravel Scoutの使用方法に関する公式ドキュメントをご覧ください。
composer test セキュリティ関連の問題を発見した場合は、問題トラッカーを使用する代わりに[email protected]にメールしてください。
最近変更されたものについては、Changelogをご覧ください。
詳細については、寄付をご覧ください。
MITライセンス(MIT)。詳細については、ライセンスファイルをご覧ください。