ejklock / laravel-cloudsearch
在Amazon的CloudSearch上索引和搜索Laravel模型。
Requires
- php: >=7.3
- aws/aws-sdk-php: ~3.0
- illuminate/config: ~5.4|~6.0|~7.0|~8.0|~9.0|~10.0
- illuminate/support: ~5.4|~6.0|~7.0|~8.0|~9.0|~10.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ~9.0|~10.0
README
在Amazon的CloudSearch上索引和搜索Laravel模型。要开始,您应该具备基本的CloudSearch工作原理知识。
安装
Composer
从命令行运行:
$ composer require torann/laravel-cloudsearch
Laravel
安装完成后,您需要将服务提供者注册到应用程序中。打开 config/app.php
并找到 providers
键。
'providers' => [ LaravelCloudSearch\LaravelCloudSearchServiceProvider::class, ]
Lumen
对于Lumen,在 bootstrap/app.php
中注册服务提供者。
$app->register(LaravelCloudSearch\LaravelCloudSearchServiceProvider::class);
发布配置
从项目的根目录运行此命令
$ php artisan vendor:publish --provider="LaravelCloudSearch\LaravelCloudSearchServiceProvider" --tag=config
配置文件将被发布到 config/cloud-search.php
。
迁移
该包使用批量队列系统来更新AWS上的文档。这样做是为了帮助减少API调用次数(从长远来看可以节省金钱)。
php artisan vendor:publish --provider="LaravelCloudSearch\LaravelCloudSearchServiceProvider" --tag=migrations
从项目的根目录运行此命令以生成存储货币的表
$ php artisan migrate
字段
为了更好地管理字段,该包提供简单的字段管理命令。这是完全可选的,因为您也可以在AWS控制台中管理它们。
注意:如果您选择不使用此命令来管理或设置您的字段,您仍然需要将字段
searchable_type
作为literal
添加。这用于存储模型类型。
它们可以在 config/cloud-search.php
文件中的 fields
属性下找到
'fields' => [ 'title' => 'text', 'status' => 'literal', ],
Artisan命令
search:fields
初始化Eloquent模型映射。
search:index <model>
要索引的模型名称或以逗号分隔的名称。
参数
model Name or comma separated names of the model(s) to index
search:flush <model>
从索引中清除所有模型文档。
参数
model Name or comma separated names of the model(s) to index
search:queue
通过排队更新和删除来减少对CloudSearch服务器的调用次数。
索引
一旦您将 LaravelCloudSearch\Eloquent\Searchable
特性添加到模型中,您所需要做的就是保存模型实例,当运行 search:queue
命令时,它将自动添加到您的索引中。
$post = new App\Post; // ... $post->save();
注意:如果模型文档已经索引,则它将被更新。如果它不存在,则会被添加。
更新文档
要更新索引模型,您只需更新模型实例的属性并将模型 `save` 到数据库中。该包将自动将更改持久化到您的搜索索引中
$post = App\Post::find(1); // Update the post... $post->save();
删除文档
要从索引中删除文档,只需从数据库中 `delete` 模型即可。这种删除方式甚至与 软删除 模型兼容
$post = App\Post::find(1); $post->delete();
搜索
您可以使用 search
方法开始搜索模型。搜索方法接受一个字符串,该字符串将被用于搜索您的模型。然后,您应该在搜索查询上链接 get
方法以检索匹配给定搜索查询的Eloquent模型
$posts = App\Post::search('Kitten fluff')->get();
由于包搜索返回Eloquent模型的集合,因此您甚至可以直接从路由或控制器返回结果,并且它们将自动转换为JSON
use Illuminate\Http\Request; Route::get('/search', function (Request $request) { return App\Post::search($request->search)->get(); });
分页
除了检索模型集合外,您还可以使用 paginate
方法分页搜索结果。该方法将返回一个与您对传统Eloquent查询分页时相同的 Paginator
实例
$posts = App\Post::search('Kitten fluff')->paginate();
您可以通过将数量作为 paginate
方法的第一个参数传递来指定每页要检索的模型数量
$posts = App\Post::search('Kitten fluff')->paginate(15);
一旦检索到结果,您就可以像传统Eloquent查询一样使用Blade显示结果和渲染页面链接
<div class="container"> @foreach ($posts as $post) {{ $post->title }} @endforeach </div> {{ $posts->links() }}
基本构建器使用
初始化构建器实例
$query = app(\LaravelCloudSearch\CloudSearcher::class)->newQuery();
您可以通过这种方式链式调用查询方法
$query->phrase('ford') ->term('National Equipment', 'seller') ->range('year', '2010');
使用 get()
或 paginate()
方法提交查询并从 AWS 获取结果。
$results = $query->get();
在上面的示例中,我们没有设置搜索类型,这意味着返回的结果将匹配 CloudSearch 域上的任何文档。要细化您的搜索以针对特定模型,您可以使用示例中显示的模型,或者使用 searchableType()
方法设置模型的类名(在模型实例调用中会自动完成此操作)
$query = app(\LaravelCloudSearch\CloudSearcher::class)->newQuery(); $results = $query->searchableType(\App\LawnMower::class) ->term('honda', 'name') ->get();
搜索查询运算符和嵌套查询
您可以使用 and
、or
和 not
运算符来构建复合和嵌套查询。相应的 and()
、or()
和 not()
方法期望一个闭包作为它们的参数。您还可以链式调用所有可用方法,并在闭包内嵌套更多子查询。
$query->or(function($builder) { $builder->phrase('ford') ->phrase('truck'); });
队列
为了减少对 CloudSearch 端点的批量请求次数(因为它们很贵),使用了队列系统。这可以在 Laravel 的 任务调度 中设置。您可以使用计划任务频率选项来决定其运行频率。请注意,这需要使用数据库来功能。
添加到 /app/Console/Kernel.php
的任务示例
/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('search:queue')->everyTenMinutes(); }
多语言
此功能为实验性
Laravel CloudSearch 可以通过将语言代码附加到索引类型来支持多种语言,因此当系统执行搜索时,它将仅查找具有当前系统区域设置后缀索引类型的当前系统语言的数据。为此,模型需要使用 LaravelCloudSearch\Eloquent\Localized
特性或类似特性。