هذه الحزمة تجعل من السهل استخدام إمكانيات البحث النصية الكاملة لـ PostgreSQL مع Laravel Scout.
إذا وجدت هذه الحزمة مفيدة ، فالرجاء النظر في قهوة لي.
يمكنك تثبيت الحزمة عبر الملحن:
composer require pmatseykanets/laravel-scout-postgresإذا كنت تستخدم Laravel <5.5 أو إذا تم إيقاف اكتشاف حزمة تلقائي ، فعليك تسجيل مزود الخدمة يدويًا:
// config/app.php
' providers ' => [
...
ScoutEngines Postgres PostgresEngineServiceProvider::class,
], يستخدم مزود خدمة Scout config_path غير المدرج في Lumen. لإصلاح ذلك ، قم بتضمين المقتطف التالي إما مباشرة في 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 );
}
} قم بإنشاء ملف تكوين scout.php في مجلد app/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 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 '
],
... تأكد من تعيين تكوين البحث عن النص الافتراضي المناسب (في postgresql.conf ) ، لقاعدة بيانات معينة ( ALTER DATABASE ... SET default_text_search_config TO ... ) أو تعيين default_text_search_config في كل جلسة.
للتحقق من القيمة الحالية
SHOW default_text_search_config; بشكل افتراضي ، يتوقع المحرك أن يتم تخزين المستندات المحسورة (بيانات النماذج) في نفس الجدول مثل النموذج في عمود searchable عن نوع tsvector . ستحتاج إلى إنشاء هذا العمود وفهرس في المخطط الخاص بك. يمكنك الاختيار بين فهارس GIN و GiST في 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 ' );
}
}بالإضافة إلى سمات النموذج ، يمكنك إحضار أي بيانات أخرى إلى مستند الفهرس. أي قائمة العلامات للنشر.
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 ' ,
];
}
}
. . . إذا قررت الاحتفاظ بفهرس النموذج الخاص بك خارج جدول النموذج ، فيمكنك إخبار المحرك بأنك تريد دفع حقول إضافية في جدول الفهرس الذي يمكنك استخدامه لتصفية النتيجة المحددة عن طريق تطبيق where() مع Builder الكشفية. في هذه الحالة ، ستحتاج إلى تنفيذ 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). يرجى الاطلاع على ملف الترخيص لمزيد من المعلومات.