allko / laravel-redis-scout
Laravel Scout 的 Redis 引擎
1.0.1
2023-08-19 15:48 UTC
Requires
- laravel/scout: ~9.0|~10.0
Suggests
- php-redis: PHP extension for Redis support (Required php extension, faster than predis)
- predis/predis: PHP library for Redis support (Easy to install)
This package is auto-updated.
Last update: 2024-09-19 17:59:14 UTC
README
关于 Laravel Redis Scout 引擎
由于 Laravel Scout 没有合适的 Redis 引擎,因此我创建了一个。在本地 Redis 实例上,对约 10k 条记录进行测试,响应时间约为 0.1 秒
安装
使用 composer 安装
composer require allko/laravel-redis-scout
.env
SCOUT_DRIVER=redis
REDIS_HOST=....
REDIS_PASSWORD=null
REDIS_PORT=6379
scout.php (如果您想更改任何内容,则需要)
<?php
return [
// ....
/*
|--------------------------------------------------------------------------
| Redis configuration
|--------------------------------------------------------------------------
|
*/
'redis' => [
/*
|--------------------------------------------------------------------------
| What connection to use
|--------------------------------------------------------------------------
|
| Decide which redis connection will be used by the Engine
|
| Read more here: https://laravel.net.cn/docs/10.x/redis
|
*/
'connection' => [
'name' => null, // use default connection
],
/*
|--------------------------------------------------------------------------
| Search method
|--------------------------------------------------------------------------
|
| Decide which search method to use when searching
|
| * STRPOS (case-sensitive https://php.ac.cn/strpos)
| * STRIPOS (DEFAULT) (case-insensitive https://php.ac.cn/stripos)
| * WILDCARD (case-insensitive preg_match but it will only accept "*" as wildcard)
| * REGEX (Can cause exceptions https://php.ac.cn/preg_match)
*/
'method' => \Allko\RedisScoutEngine\SearchMethods::STRIPOS
/*
|--------------------------------------------------------------------------
| orderBy sort options
|--------------------------------------------------------------------------
|
| Read more about sort options on PHPs official docs
|
| https://php.ac.cn/manual/en/function.sort.php
*/
'sort_options' => SORT_NATURAL
]
];
用法
有关用法,请参阅 官方文档
搜索回调
如果您想对 get
和 paginate
的结果进行筛选,可以使用 \Allko\RedisScoutEngine\Callback
use App\Models\User;
use Allko\RedisScoutEngine\Callback;
User::search('xxxx', fn(Callback $cb) => $cb->mapResult(fn(User $user) => ['id' => $user->id, 'name' => $user->name, 'abc' => 123]))->paginate()
{
"current_page":1,
"data":[
{
"id":1,
"name":"Kade Trantow",
"abc":123
},
{
"id":73,
"name":"Kaden Gulgowski",
"abc":123
},
{
"id":722,
"name":"Kade Goyette",
"abc":123
},
{
"id":1836,
"name":"Dr. Kade Ankunding",
"abc":123
},
{
"id":3260,
"name":"Kade Murray",
"abc":123
},
{
"id":8916,
"name":"Prof. Kade Howe",
"abc":123
},
{
"id":9889,
"name":"Kade Spinka",
"abc":123
}
],
"first_page_url":"http:\/\/localhost?query=kade&page=1",
"from":1,
"last_page":1,
"last_page_url":"http:\/\/localhost?query=kade&page=1",
"links":[
{
"url":null,
"label":"« Previous",
"active":false
},
{
"url":"http:\/\/localhost?query=kade&page=1",
"label":"1",
"active":true
},
{
"url":null,
"label":"Next »",
"active":false
}
],
"next_page_url":null,
"path":"http:\/\/localhost",
"per_page":15,
"prev_page_url":null,
"to":7,
"total":7
}