irto/solrio

此包已被弃用且不再维护。未建议替代包。

Laravel 5.1 包,基于 Solarium 实现对 Eloquent 模型的全文搜索

v0.1.0-alpha5 2015-10-27 20:05 UTC

This package is not auto-updated.

Last update: 2023-07-03 22:01:20 UTC


README

Latest Stable Version Latest Unstable Version License

Laravel 5.1 包,基于 Solarium 实现对 Eloquent 模型的全文搜索,灵感来源于 Laravel Lucene Search

安装

在 composer.json 中要求此包,并运行 composer update

    {
        "require": {
            "irto/solrio": "0.*"
        }
    }

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

    'providers' => [
        Irto\Solrio\ServiceProvider::class,
    ],

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

    'aliases' => [
        'Search' => Irto\Solrio\Facade::class,
    ],

##配置

通过运行以下命令将配置文件发布到您的项目中

    php artisan vendor:publish --provider="Irto\Solrio\ServiceProvider"

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

    'index' => [
        
        'models' => [

            // ...

            namespace\FirstModel::class => [
                'fields' => [
                    'name', 'full_description', // Fields for indexing.
                ]
            ],

            namespace\SecondModel::class => [
                'fields' => [
                    'name', 'short_description', // Fields for indexing.
                ]
            ],

            // ...

        ],
    ],

##用法

###Artisan 命令

####构建/重建搜索索引

要构建搜索索引,运行

    php artisan search:rebuild

####清除搜索索引

要清除搜索索引,运行

    php artisan search:clear

###部分更新搜索索引

要注册必要的事件(保存/更新/删除),请使用 Irto\Solrio\Model\SearchTrait 在目标模型中

    use Illuminate\Database\Eloquent\Model;

    use Irto\Solrio\Model\Searchable;
    use Irto\Solrio\Model\SearchTrait;

    class Dummy extends Model implements Searchable // use of Searchable is optional, without this will be always available to search
    {
        use SearchTrait;

        /**
         * Is the model available for searching?
         */
        public function isSearchable()
        {
            return $this->publish;
        }
    }

您也可以手动操作,例如在一个队列监听器中

    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;

    use Search; // if alias is configured

    class DummyUpdatedListener extends ShouldQueue
    {
        use InteractsWithQueue;

        public function handle($event)
        {
            $model = $event->getModel();

            Search::update($model); // you can use 'App::offsetGet('search')->update($model);' instead
        }
    }