Laravel 5.5 套件,用於基於 ZendSearch Lucene 對 Eloquent 模型進行全文搜尋。
在您的composer.json中需要此套件並執行composer update:
{
"require" : {
"nqxcode/laravel-lucene-search" : " 2.4.* "
}
}
更新composer後,將ServiceProvider加入config/app.php
中的providers陣列中
' 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 術語中的文件層級提升。預設情況下,所有模型的boost值都等於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 );
匹配突出顯示適用於以utf-8編碼的任何 html 片段,並且僅針對最後執行的請求執行。
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 許可證獲得許可。