Laravel框架redis搜索引擎扩展 Laravel-RediSearch

Laravel-RediSearch允许索引和搜索Laravel模型。它提供了Laravel Scout驱动程序。

安装

composer require ethanhann/laravel-redisearch

注册提供商
将此条目添加到config / app.php中的providers数组中。

Ehann\LaravelRediSearch\RediSearchServiceProvider::class

配置侦查驱动程序
在config / scout.php中更新Scout驱动程序。

'driver' => env('SCOUT_DRIVER', 'ehann-redisearch'),

定义可搜索架构
定义将在索引上使用的字段类型

<?php

namespace App;

use Laravel\Scout\Searchable;
...
use Ehann\RediSearch\Fields\TextField;
use Ehann\RediSearch\Fields\GeoField;
use Ehann\RediSearch\Fields\NumericField;
use Ehann\RediSearch\Fields\TagField;
use Ehann\RediSearch\Fields\GeoLocation;
...

class User extends Model {
    use Searchable;

    public function searchableAs()
    {
        return "user_index";
    }

    public function toSearchableArray()
    {
        return [
            "name" => $this->name,
            "username" => $this->username,
            "location" => new GeoLocation(
                                $this->longitude,
                                $this->latitude
                            )
            "age" => $this->age,
       ];
    }

    public function searchableSchema()
    {
        return [
            "name" => TextField::class,
            "username" => TextField::class,
            "location" => GeoField::class,
            "age" => NumericField::class
      ];
    }
}

导入模型
导入配置为可搜索的“产品”模型:

artisan ehann:redisearch:import App\\Product

导入前删除索引:

artisan ehann:redisearch:import App\\Product --recreate-index

导入没有ID字段的模型(应该很少使用):

artisan ehann:redisearch:import App\\Product --no-id

查询过滤器
如何查询过滤器?过滤标签字段

App\User::search("Search Query", function($index){
    return $filter->geoFilter("location", 5.56475, 5.75516, 100)
                  ->numericFilter('age', 18, 32)
})->get()

有关其他信息,请参见Laravel Scout文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值