该软件包使使用Laravel Scout的本机PostgreSQL全文搜索功能变得易于使用。
如果您发现此包装有用,请考虑给我喝咖啡。
您可以通过作曲家安装软件包:
composer require pmatseykanets/laravel-scout-postgres如果您使用的是Laravel <5.5,或者您的软件包自动发现关闭了,则必须手动注册服务提供商:
// config/app.php
' providers ' => [
...
ScoutEngines Postgres PostgresEngineServiceProvider::class,
],侦察服务提供商使用不包含在管腔中的config_path助手。为了解决此问题,包括直接在bootstrap.app中或自动加载帮助者文件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配置文件,并使用以下内容
<?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 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 '
],
...确保将适当的默认文本搜索配置设置为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 ' );
}
}除了模型的属性外,您还可以将其他任何数据带到索引文档。即帖子的标签列表。
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最近发生了什么变化。
请有关详细信息,请参阅贡献。
麻省理工学院许可证(麻省理工学院)。请参阅许可证文件以获取更多信息。