حزمة 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
],
في النموذج أضف الملحق التالي:
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
]
في النموذج أضف الملحق التالي:
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
],
في النموذج أضف الملحق التالي:
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
الطرق، من الممكن ضبط الخيارات التالية:
ابحث عن جميع النماذج التي يحتوي فيها أي حقل على عبارة مثل "عبارة مركبة مكونة من عبارة واحدة":
$ 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.