แพ็คเกจ Laravel 5.5 สำหรับการค้นหาข้อความแบบเต็มผ่านโมเดล Eloquent ที่ใช้ ZendSearch Lucene
ต้องการแพ็คเกจนี้ใน composer.json ของคุณและรันการอัปเดตผู้แต่ง:
{
"require" : {
"nqxcode/laravel-lucene-search" : " 2.4.* "
}
}
หลังจากอัปเดตผู้แต่งแล้ว ให้เพิ่ม ServiceProvider ไปยังอาร์เรย์ของผู้ให้บริการใน config/app.php
' providers ' => [
Nqxcode LuceneSearch ServiceProvider ::class,
],
หากคุณต้องการใช้ส่วนหน้าเพื่อค้นหา ให้เพิ่มสิ่งนี้ลงในส่วนหน้าของคุณใน config/app.php
:
' aliases ' => [
' Search ' => Nqxcode LuceneSearch Facade ::class,
],
เผยแพร่ไฟล์กำหนดค่าลงในโปรเจ็กต์ของคุณโดยเรียกใช้:
php artisan vendor:publish --provider= " NqxcodeLuceneSearchServiceProvider "
ในไฟล์กำหนดค่าที่เผยแพร่ ให้เพิ่มคำอธิบายสำหรับโมเดลที่ต้องจัดทำดัชนี เช่น:
' index ' => [
// ...
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' , // fields for indexing
]
],
namespace SecondModel ::class => [
' fields ' => [
' name ' , ' short_description ' , // fields for indexing
]
],
namespace ModelWithCustomPrimaryKey ::class => [
// You can also define your primary key (if you use something else than "id")
' primary_key ' => ' my_custom_field_name ' ,
' fields ' => [
' username ' , ' short_description ' , // fields for indexing
]
],
// ...
],
คุณยังสามารถจัดทำดัชนีค่าของ ฟิลด์ตัวเลือกได้ (ฟิลด์ไดนามิก) สำหรับการเปิดใช้งานการจัดทำดัชนีสำหรับฟิลด์เสริม:
' optional_attributes ' => true
// or
' optional_attributes ' => [
' accessor ' => ' custom_name ' // with specifying of accessor name
]
field-name => field-value
โดยค่าเริ่มต้น ตัวเข้าถึง getOptionalAttributesAttribute
จะถูกใช้งาน ในกรณีที่ชื่อตัวเข้าถึงที่ระบุในตัวเข้าถึงการกำหนดค่า getCustomNameAttribute
จะถูกนำมาใช้ตัวอย่าง:
ในไฟล์กำหนดค่า:
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' , // fixed fields for indexing
],
' optional_attributes ' => true // enable indexing for dynamic fields
],
ในโมเดลให้เพิ่ม accessor ต่อไปนี้:
public function getOptionalAttributesAttribute ()
{
return [
' optional_attribute1 ' => ' value1 ' ,
' optional_attribute2 ' => ' value2 ' ,
];
}
ดูรายละเอียดเกี่ยวกับ Apache Lucene - การให้คะแนน
นี่คือ การเพิ่มระดับเอกสาร ในคำศัพท์ของ Apache Lucene โดยค่าเริ่มต้น ทุกรุ่นจะมีค่า บูสต์ เท่ากับ 1 สำหรับการเปลี่ยนแปลงลักษณะการทำงานนี้ ปรับแต่งบูสต์สำหรับโมเดลที่จำเป็นตามตัวอย่างต่อไปนี้
' boost ' => true
// or
' boost ' => [
' accessor ' => ' custom_name ' // with specifying of accessor name
]
ในโมเดลให้เพิ่ม accessor ต่อไปนี้:
public function getBoostAttribute ()
{
return 0.5 ; // customize boost value for model
}
getBoostAttribute
จะถูกใช้งาน ในกรณีที่ชื่อตัวเข้าถึงที่ระบุในตัวเข้าถึงการกำหนดค่า getCustomNameAttribute
จะถูกนำมาใช้ตัวอย่าง:
ในไฟล์กำหนดค่า:
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' ,
],
' boost ' => true // enable boosting for model
],
ในโมเดลให้เพิ่ม accessor ต่อไปนี้:
public function getBoostAttribute ()
{
return 0.5 ; // customize boost value for model
}
นี่คือ การเพิ่มระดับฟิลด์ของเอกสาร ในด้านคำศัพท์ของ Apache Lucene โดยค่าเริ่มต้น บูสต์ จะตั้งค่าไว้ที่ 1 สำหรับแต่ละฟิลด์ สำหรับการเปลี่ยนแปลงลักษณะการทำงานนี้ ให้ตั้งค่าบูสต์สำหรับฟิลด์ที่จำเป็นดังตัวอย่างต่อไปนี้
ในไฟล์กำหนดค่า:
namespace FirstModel ::class => [
' fields ' => [
' name ' , // field with default boost
' full_description ' => [ ' boost ' => 0.2 ], // customize boost value
],
],
หรือ/และในตัวเข้าถึงโมเดล:
public function getOptionalAttributesAttribute ()
{
return [
' optional_attribute1 ' => ' value1 ' , // field with default boost
' optional_attribute2 ' => [ ' boost ' => 0.5 , ' value ' => ' value2 ' ], // customize boost value
];
}
ตามค่าเริ่มต้น ตัวกรองต่อไปนี้จะถูกใช้ในการค้นหา:
ตัวกรองนี้สามารถลบหรือแทนที่ด้วยตัวกรองอื่นได้
' analyzer ' => [
' filters ' => [
// Default stemming filter.
Nqxcode Stemming TokenFilterEnRu ::class,
],
// List of paths to files with stopwords.
' stopwords ' => Nqxcode LuceneSearch Analyzer Stopwords Files :: get (),
],
สำหรับการสร้างดัชนีการค้นหาให้รัน:
php artisan search:rebuild --verbose
สำหรับการล้างดัชนีการค้นหาให้รัน:
php artisan search:clear
สำหรับการกรองโมเดลในผลการค้นหา แต่ละคลาสของโมเดลสามารถใช้ SearchableInterface
ได้ ตัวอย่างเช่น:
use Illuminate Database Eloquent Model ;
use Nqxcode LuceneSearch Model SearchableInterface ;
class Dummy extends Model implements SearchableInterface
{
// ...
/**
* Get id list for all searchable models.
*/
public static function searchableIds ()
{
return self :: wherePublish ( true )-> pluck ( ' id ' );
}
// ...
}
สำหรับการลงทะเบียนเหตุการณ์ที่จำเป็น (บันทึก/อัปเดต/ลบ) use NqxcodeLuceneSearchModelSearchTrait
ในโมเดลเป้าหมาย:
use Illuminate Database Eloquent Model ;
use Nqxcode LuceneSearch Model SearchableInterface ;
use Nqxcode LuceneSearch Model SearchTrait ;
class Dummy extends Model implements SearchableInterface
{
use SearchTrait ;
// ...
}
หากคุณต้องการหลีกเลี่ยงการทริกเกอร์การจัดทำดัชนี ให้รวมการดำเนินการที่จำเป็นไว้ในเมธอด withoutSyncingToSearch()
บนโมเดลของคุณ:
Product :: withoutSyncingToSearch ( function () {
// mass update position for product, e.g.
foreach ( Product :: all () as $ i => $ product ) {
$ product -> update ([ ' position ' => $ i )]);
}
});
สร้างแบบสอบถามได้หลายวิธี:
ตามค่าเริ่มต้น ข้อความค้นหาที่จะดำเนินการค้นหาใน วลีจะถูกสร้างขึ้นทั้งหมด
$ query = Search :: query ( ' clock ' ); // search by all fields.
// or
$ query = Search :: where ( ' name ' , ' clock ' ); // search by 'name' field.
// or
$ query = Search :: query ( ' clock ' ) // search by all fields with
-> where ( ' short_description ' , ' analog ' ); // filter by 'short_description' field.
// or
$ query = Product :: search ( ' clock ' ); // search only in `Product` model by all fields in case when `Product` use `SearchableTrait`.
สำหรับ query
และวิธีการ where
คุณสามารถตั้งค่าตัวเลือกต่อไปนี้:
ค้นหาโมเดลทั้งหมดที่มีวลีเช่น 'composite one twophrase' ในฟิลด์ใดๆ:
$ query = Search :: query ( ' composite phrase ' , ' * ' , [ ' proximity ' => 2 ]);
ค้นหาตามแต่ละคำในแบบสอบถาม:
$ query = Search :: query ( ' composite phrase ' , ' * ' , [ ' phrase ' => false ]);
$ query = Search :: rawQuery ( ' short_description:"analog" ' );
// or
$ rawQuery = QueryParser :: parse ( ' short_description:"analog" ' );
$ query = Search :: rawQuery ( $ rawQuery );
สำหรับแบบสอบถามที่สร้างขึ้นมีการดำเนินการต่อไปนี้:
$ models = $ query -> get ();
$ count = $ query -> count ();
$ models = $ query -> limit ( 5 , 10 )-> get (); // Limit = 5 and offset = 10
$ paginator = $ query -> paginate ( 50 );
การไฮไลต์การจับคู่พร้อมใช้งานสำหรับแฟรกเมนต์ html ใดๆ ที่เข้ารหัสใน utf-8 และดำเนินการเฉพาะกับคำขอที่ดำเนินการครั้งล่าสุดเท่านั้น
Search :: find ( ' nearly all words must be highlighted ' )-> get ();
$ highlighted = Search :: highlight ( ' all words ' );
// highlighted html:
// '<span class="highlight">all</span> <span class="highlight">words</span>'
แพ็คเกจได้รับอนุญาตภายใต้ใบอนุญาต MIT