aboutboy / xunsearch
让 Laravel 项目中使用 xunsearch 更简单
dev-master
2022-11-04 02:48 UTC
Requires
- php: >=5.3.0
- ext-mbstring: *
- lib-pcre: *
- illuminate/support: >=5.0
This package is auto-updated.
Last update: 2024-09-04 06:30:54 UTC
README
基于 XunSearch 的 Laravel 9.* 包,用于在 Eloquent 模型上执行全文搜索。
安装
在 composer.json 中添加此包并运行 composer update
{ "require": { "aboutboy/xunsearch": "dev-master" } }
更新 composer 后,将 ServiceProvider 添加到 app/config/app.php
中的 providers 数组
'providers' => [ Aboutboy\XunSearch\ServiceProvider::class, ],
如果您想使用外观搜索,请将以下内容添加到 app/config/app.php
中的 facades
'aliases' => [ 'Search' => Easy\XunSearch\Facade::class, ],
配置
通过运行以下命令将配置文件发布到您的项目中
php artisan vendor:publish --provider="Aboutboy\Xunsearch\ServiceProvider"
### 在发布的配置文件中添加需要索引的模型的描述,例如
//@see http://www.xunsearch.com/doc/php/guide/ini.guide "project" => [ "project.name" => "easy", "project.default_charset" => "utf-8", "server.index" => "127.0.0.1:8383", "server.search" => "127.0.0.1:8384", //remember change FIELD_LABEL_DEFAULT_SEARCH_PK value in Config.php "primary_key" => [ "type" => "id" ], //remember change FIELD_LABEL_DEFAULT_CLASS_ID value in Config.php "class_uid" => [ "index" => "both" ], //remember change FIELD_LABEL_DEFAULT_DB_PK value in Config.php "id" => [ "type" => "numeric" ], "subject" => [ "type" => "title" ], "status" => [ 'type' => "numeric" ], "content" => [ "type" => "body" ] ], 'index' => [ // ... namespace\FirstModel::class => [ 'fields' => [ 'name', 'full_description', // fields for indexing ], 'primary_key' => 'id' //primary_key name in DB, default 'id' ], namespace\SecondModel::class => [ 'fields' => [ 'name', 'short_description', // fields for indexing ] ], // ... ],
用法
Artisan 命令
初始化或重建搜索索引
要构建搜索索引,请运行
php artisan search:rebuild --verbose
清除搜索索引
要清除搜索索引,请运行
php artisan search:clear
搜索结果中的模型过滤
为了过滤搜索结果中的模型,每个模型的类都可以实现 SearchableInterface
。例如
use Illuminate\Database\Eloquent\Model; use DavinBao\LaravelXunSearch\Model\SearchableInterface; class Dummy extends Model implements SearchableInterface { // ... /** * Get id list for all searchable models. */ public static function searchableIds() { return self::wherePublish(true)->lists('id'); } // ... }
部分更新搜索索引
为了注册必要的活动(保存/更新/删除),在目标模型中使用 use DavinBao\LaravelXunSearch\Model\SearchTrait
use Illuminate\Database\Eloquent\Model; use DavinBao\LaravelXunSearch\Model\SearchableInterface; use DavinBao\LaravelXunSearch\Model\SearchTrait; class Dummy extends Model implements SearchableInterface { use SearchTrait; // ... }
查询构建
以几种方式构建查询
使用构造函数
默认情况下,将创建将执行完全短语搜索的查询。
简单查询
$query = Model::getSearch()->addQuery("clock"); // search by all fields. // or $query = Model::getSearch()->addQuery('name:clock'); // search by 'name' field. // or $query = Model::getSearch()->addQuery('name:clock'); // filter by 'short_description' field. $Ids = Model::getSearch()->addQuery('name:clock')->getIDList(); // filter by 'short_description' field.
获取结果
对于构建的查询,以下操作可用
获取所有找到的模型
$models = $query->search();
获取所有找到的模型 ID
$models = $query->getIDList();
获取结果计数
$count = $query->count();
获取带有偏移量的限制结果
$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10
排序
$query = $query->setSort('chrono', true);
匹配项高亮显示
匹配项高亮显示对于任何以 utf-8 编码的 HTML 片段都可用,并且仅对最后一次执行的请求执行。
$docs = $search->setQuery('测试')->setLimit(5)->search(); foreach ($docs as $doc) { $subject = $search->highlight($doc->subject); // 高亮处理 subject 字段 $message = $search->highlight($doc->message); // 高亮处理 message 字段 echo $doc->rank() . '. ' . $subject . " [" . $doc->percent() . "%] - "; echo date("Y-m-d", $doc->chrono) . "\n" . $message . "\n"; }
许可
该包根据 MIT 许可证授权。