Mit diesem Paket können Sie native PostgreSQL -Volltext -Suchfunktionen mit Laravel Scout einfach verwenden.
Wenn Sie dieses Paket nützlich finden, erwägen Sie mir bitte einen Kaffee.
Sie können das Paket über Komponist installieren:
composer require pmatseykanets/laravel-scout-postgresWenn Sie Laravel <5.5 verwenden oder ein Paket für automatische Entdeckungen ausgeschaltet haben, müssen Sie den Dienstanbieter manuell registrieren:
// config/app.php
' providers ' => [
...
ScoutEngines Postgres PostgresEngineServiceProvider::class,
], Der Scout -Dienstanbieter verwendet config_path helfer, der nicht in Lumen enthalten ist. Um dies zu beheben, beinhalten Sie den folgenden Snippet entweder direkt in bootstrap.app oder in Ihrer automatoaden Helpers Datei 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 );
}
} Erstellen Sie die Konfigurationsdatei scout.php im app/config mit den folgenden Inhalten
<?php
return [
' driver ' => env ( ' SCOUT_DRIVER ' , ' pgsql ' ),
' prefix ' => env ( ' SCOUT_PREFIX ' , '' ),
' queue ' => false ,
' pgsql ' => [
' connection ' => ' pgsql ' ,
' maintain_index ' => true ,
' config ' => ' english ' ,
],
];Dienstleister anmelden:
// bootstrap/app.php
$ app -> register ( Laravel Scout ScoutServiceProvider::class);
$ app -> configure ( ' scout ' );
$ app -> register ( ScoutEngines Postgres PostgresEngineServiceProvider::class); Geben Sie die Datenbankverbindung an, die zum Zugriff auf indizierte Dokumente in der Laravel Scout -Konfigurationsdatei config/scout.php verwendet werden sollte:
// 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 '
],
... Stellen Sie sicher, dass eine geeignete Standard -Textsuche -Konfiguration GlobBaly (in postgresql.conf ) für eine bestimmte Datenbank ( ALTER DATABASE ... SET default_text_search_config TO ... ) fest oder setzen Sie default_text_search_config in jeder Sitzung alternativ fest.
Um den aktuellen Wert zu überprüfen
SHOW default_text_search_config; Standardmäßig erwartet die Engine, dass Parsen -Dokumente (Modelldaten) in derselben Tabelle wie das Modell in einer searchable des Typs tsvector gespeichert werden. Sie müssten diese Spalte und einen Index in Ihrem Schema erstellen. Sie können zwischen GIN und GiST -Indizes in PostgreSQL wählen.
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 ' );
}
}Zusätzlich zu den Attributen des Modells können Sie andere andere Daten in das Indexdokument bringen. Dh eine Liste von Tags für einen Beitrag.
public function toSearchableArray ()
{
return [
' title ' => $ this -> title ,
' content ' => $ this -> content ,
' author ' => $ this -> user -> name ,
' tags ' => $ this -> tags -> pluck ( ' tag ' )-> implode ( ' ' ),
];
} Sie können das Motorverhalten für ein bestimmtes Modell fein einstellen, indem Sie searchableOptions() in Ihrem Modell implementieren.
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 ' ,
];
}
}
. . . Wenn Sie sich dafür entscheiden, den Index Ihres Modells außerhalb der Tabelle des Modells zu halten, können Sie die Engine wissen lassen, dass Sie zusätzliche Felder in die Indextabelle drücken möchten, mit der Sie das Ergebnis festlegen können, indem Sie where() mit dem Scout Builder anwenden. In diesem Fall müssen Sie searchableAdditionalArray() in Ihrem Modell implementieren. Natürlich sollte das Schema für die externe Tabelle diese zusätzlichen Spalten enthalten.
public function searchableAdditionalArray ()
{
return [
' user_id ' => $ this -> user_id ,
];
}Möglicherweise möchten Sie Ihre durchsuchbare Spalte versteckt, damit sie Ihnen nicht im Weg steht
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 ();Bitte beachten Sie die offizielle Dokumentation zur Verwendung von Laravel Scout.
composer test Wenn Sie Sicherheitsprobleme entdecken, senden Sie bitte eine E -Mail an [email protected], anstatt den Ausgabe -Tracker zu verwenden.
Weitere Informationen finden Sie in letzter Zeit, was in letzter Zeit geändert wurde.
Weitere Informationen finden Sie unter Beitrag.
Die MIT -Lizenz (MIT). Weitere Informationen finden Sie unter Lizenzdatei.