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 许可证获得许可。