แพ็คเกจนี้ทำให้ใช้งานง่าย PostgreSQL ความสามารถในการค้นหาข้อความแบบเต็มกับ Laravel Scout
หากคุณพบว่าแพ็คเกจนี้เป็นประโยชน์โปรดพิจารณาโดยดื่มกาแฟ
คุณสามารถติดตั้งแพ็คเกจผ่านนักแต่งเพลง:
composer require pmatseykanets/laravel-scout-postgresหากคุณใช้ Laravel <5.5 หรือหากคุณปิดการค้นพบแบบอัตโนมัติแพ็คเกจคุณต้องลงทะเบียนผู้ให้บริการด้วยตนเอง:
// config/app.php
' providers ' => [
...
ScoutEngines Postgres PostgresEngineServiceProvider::class,
], ผู้ให้บริการลูกเสือใช้ตัวช่วย config_path ที่ไม่รวมอยู่ในลูเมน ในการแก้ไขสิ่งนี้รวมถึงตัวอย่างต่อไปนี้โดยตรงใน bootstrap.app หรือในไฟล์ผู้ช่วยที่โหลดอัตโนมัติของคุณเช่น 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 );
}
} สร้างไฟล์ config.php 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 '
],
... ตรวจสอบให้แน่ใจว่าการกำหนดค่าการค้นหาข้อความเริ่มต้นที่เหมาะสมถูกตั้งค่า globbaly (ใน postgresql.conf ) สำหรับฐานข้อมูลเฉพาะ ( ALTER DATABASE ... SET default_text_search_config TO ... ) หรือตั้งค่า default_text_search_config ในแต่ละเซสชัน
เพื่อตรวจสอบค่าปัจจุบัน
SHOW default_text_search_config; โดยค่าเริ่มต้นเครื่องยนต์คาดว่าเอกสารที่แยกวิเคราะห์ (ข้อมูลโมเดล) จะถูกเก็บไว้ในตารางเดียวกันกับรุ่นในคอลัมน์ searchable ของ Type 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 (MIT) โปรดดูไฟล์ใบอนุญาตสำหรับข้อมูลเพิ่มเติม