tarre / laravel-redis-scout-engine
为 Laravel Scout 提供的 Redis 引擎
1.3.1
2024-03-15 00:04 UTC
Requires
- laravel/scout: ~9.0|~10.0|~11.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)
README
关于 Laravel Redis Scout 引擎
由于 Laravel Scout 没有合适的 Redis 引擎,我创建了一个。测试了大约 10,000 条记录,在本地 Redis 实例上的响应时间约为 0.1 秒。
安装
使用 composer 安装
composer require tarre/laravel-redis-scout-engine
.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 ], /* |-------------------------------------------------------------------------- | Chunk size for redis hScan |-------------------------------------------------------------------------- | | | Read more here: https://redis.ac.cn/commands/hscan | */ 'scan_chunk' => 1000, /* |-------------------------------------------------------------------------- | 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' => \Tarre\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 的结果进行过滤,可以使用 \Tarre\RedisScoutEngine\Callback
use App\Models\User; use Tarre\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
}