Download the PHP package whereof/think-scout without Composer

On this page you can find all versions of the php package whereof/think-scout. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package think-scout

前言

tp5er/think-scout根据thinkphp设计思想参考laravel/scout进行扩展

tp5er/think-scout 为模型的全文搜索提供了一个简单的、基于驱动程序的解决方案。

目前,Scout 自带了一个 Elasticsearch 驱动;而编写自定义驱动程序很简单,你可以自由地使用自己的搜索实现来扩展 Scout。

命令行使用

//创建模型索引
php think scout:index "app\\model\\User"
//删除模型的索引
php think scout:delete-index "app\\model\\User"
//分批将模型中的数据同步到引擎中
php think scout:import "app\\model\\User"
//清空引擎中的数据(危险操作慎重使用)
php think scout:flush "app\\model\\User"

Scout事件

事件 描述 事件使用方法名
onScoutUpdated 注册模型新增/更新事件 Event::trigger('onScoutUpdated', User::find(1));
onScoutDeleted 注册模型删除事件 Event::trigger('onScoutDeleted', User::find(1));
onScoutImported 注册模型全量新增/更新 Event::trigger('onScoutImported', new User());
onScoutFlushed 注册模型全量删除数据 Event::trigger('onScoutFlushed', new User());
onScoutCreateIndex 注册模型索引创建 Event::trigger('onScoutCreateIndex', new User());
onScoutDeleteIndex 注册模型索引删除 Event::trigger('onScoutDeleteIndex', new User());

模型事件

官网参考地址:https://www.kancloud.cn/manual/thinkphp6_0/1037598

事件 描述 事件方法名
after_read 查询后 onAfterRead
before_insert 新增前 onBeforeInsert
after_insert 新增后 onAfterInsert
before_update 更新前 onBeforeUpdate
after_update 更新后 onAfterUpdate
before_write 写入前 onBeforeWrite
after_write 写入后 onAfterWrite
before_delete 删除前 onBeforeDelete
after_delete 删除后 onAfterDelete
before_restore 恢复前 onBeforeRestore
after_restore 恢复后 onAfterRestore

注册Scout事件到模型事件中

如果需要通过模型事件来自动完成Scout数据增量同步到引擎中需要进行手动注册

重要事情说三遍:

手动注册Scout事件到模型事件中!

手动注册Scout事件到模型事件中!

手动注册Scout事件到模型事件中!

手动依次注册

最佳的方式是使用队列

安装

引入组件包和 Elasticsearch 驱动

composer require tp5er/think-scout
// 根据自己的elasticsearch版本进行安装
composer require elasticsearch/elasticsearch

最后,在你要做搜索的模型中添加tp5er\think\scout\Searchable trait。这个 trait 会注册一个模型观察者来保持模型和所有驱动的同步:

配置文件config/scout.php

暂停索引

搜索

你可以使用 search 方法来搜索模型。search 方法接受一个用于搜索模型的字符串。你还需在搜索查询上链式调用 get 方法,才能用给定的搜索语句查询与之匹配的模型模型:

User::search()->get();

如果你想在它们返回模型模型前得到原结果,你应该使用raw 方法:

搜索查询通常会在模型的 searchableAs 方法指定的索引上执行。当然,你也可以使用 within 方法指定应该搜索的自定义索引:

User::search()->within('tp5er_user')->get();

Where 语句

Scout 允许你在搜索查询中增加简单的「where」语句。目前,这些语句只支持基本的数值等式检查,并且主要是用于根据拥有者的 ID 进行的范围搜索查询。由于搜索索引不是关系型数据库,因此当前不支持更高级的「where」语句:

User::search()->where('user_id', 1)->get();

分页

除了检索模型的集合,你也可以使用 paginate 方法对搜索结果进行分页。这个方法会返回一个就像 传统的模型查询分页 一样的 Paginator 实例:

User::search()->paginate();

你可以通过将数量作为第一个参数传递给 paginate 方法来指定每页检索多少个模型:

User::search()->paginate(15);

自定义引擎

如果内置的 Scout 搜索引擎不能满足你的需求,你可以写自定义的引擎并且将它注册到 Scout。你的引擎需要继承 tp5er\think\scout\Engine 抽象类,这个抽象类包含了你自定义的引擎必须要实现的十种抽象方法:

abstract public function update(Collection $models): void;
abstract public function delete(Collection $models): void;
abstract public function search(Builder $builder);
abstract public function paginate(Builder $builder, int $perPage, int $page);
abstract public function mapIds($results): Collection;
abstract public function map(Builder $builder, $results, Model $model): Collection;
abstract public function getTotalCount($results): int;
abstract public function flush(Model $model): void;

注册引擎

一旦你写好了自定义引擎,您就可以在配置文件中指定引擎了。举个例子,如果你写好了一个 MySqlSearchEngine,您就可以在配置文件中这样写:

<?php

return [
    'default'     => env('SCOUT_DRIVER', 'mysql'),
    'soft_delete' => false,
    'engine'      => [
        'mysql'         => [
            'driver' => MySqlSearchEngine::class,
            'prefix' => '',
        ]
    ],
];

All versions of think-scout with dependencies

PHP Build Version
Package Version
Requires php Version >7.1
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package whereof/think-scout contains the following files

Loading the files please wait ....