davin-bao/laravel-xun-search

Laravel 5 全文搜索包,基于XunSearch对Eloquent模型进行全文搜索。

dev-master 2017-03-30 02:57 UTC

This package is auto-updated.

Last update: 2024-09-21 19:50:21 UTC


README

Latest Stable Version Latest Unstable Version License Total Downloads

Laravel 5.1 全文搜索包,基于XunSearch对Eloquent模型进行全文搜索。

安装

在您的composer.json中添加此包并运行composer update

{
	"require": {
        "davin-bao/laravel-xun-search": "dev-master"
	}
}

更新composer后,将ServiceProvider添加到app/config/app.php中的providers数组

'providers' => [
	DavinBao\LaravelXunSearch\ServiceProvider::class,
],

如果想要使用外观来搜索,请将以下内容添加到app/config/app.php中的facades

'aliases' => [
	'Search' => DavinBao\LaravelXunSearch\Facade::class,
],

配置

运行以下命令将配置文件发布到项目中:

php artisan vendor:publish --provider="DavinBao\LaravelXunSearch\ServiceProvider"

###在发布的配置文件中为需要索引的模型添加描述,例如

//@see http://www.xunsearch.com/doc/php/guide/ini.guide
"project" => [
    "project.name" => "demo",
    "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"
    ],
    "username" => [
        "type" => "title"
    ],
    "email" => [
        "index" => "both"
    ],
    "last_seen" => [
        "type" => "numeric"
    ],
    "role" => [
        "index" => "both"
    ],
    "uri" => [
        "index" => "both"
    ],
    "action" => [
        "index" => "both"
    ],
],

'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许可证下。