使用Elasticsearch完成商品搜索
最近开发的项目需要收集商品信息,顾名思义,又是一个搜索巨多的活,果断上ES。
一. 安装扩展
首先,通过 Composer 包管理器来安装 Scout:
composer require laravel/scout
这是官方的扩展,这个驱动对我们来说不太友好,我需要别的驱动,我选择 scout-elasticsearch-driver
composer require babenkoivan/scout-elasticsearch-driver
如果您使用的是 Laravel
版本 <= 5.4,请在 config/app.php
中添加以下
'providers' => [ | |
Laravel\Scout\ScoutServiceProvider::class, | |
ScoutElastic\ScoutElasticServiceProvider::class, | |
] |
全部安装完成后,使用命令来生成 Scout 配置文件。这个命令将在你的 config
目录下生成 scout.php
以及scout_elastic.php
配置文件。
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" | |
php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider" |
二. es配置
在.env
文件添加
// 驱动的host信息,如果有账号密码:http://elastic_user:password@127.0.0.1:9200 | |
SCOUT_ELASTIC_HOST=elasticsearch:9200 | |
// 驱动 | |
SCOUT_DRIVER=elastic | |
// 是否开启队列,默认不需要配置,建议开启 | |
SCOUT_QUEUE=true |
三. 索引配置
索引配置器用于为 Elasticsearch
设置索引。这里我需要一个商品的索引,请使用以下 artisan
命令:
php artisan elastic:create-index "App\Elasticsearch\IndexConfigurators\ProductIndexConfigurator"
执行成功后需要配置商品的索引
namespace App\Elasticsearch\IndexConfigurators; | |
use ScoutElastic\IndexConfigurator; | |
use ScoutElastic\Migratable; | |
class ProductIndexConfigurator extends IndexConfigurator | |
{ | |
use Migratable; | |
protected $name = 'product_index'; | |
// You can specify any settings you want, for example, analyzers. | |
protected $settings = [ | |
'number_of_shards' => 5, | |
'number_of_replicas' => 0, | |
'max_result_window' => 100000000 | |
]; | |
protected $defaultMapping = [ | |
'properties' => [ | |
'id' => [ | |
'type' => 'integer', | |
], | |
'title' => [ | |
'type' => 'text', | |
'analyzer' => 'ik_max_word', | |
'search_analyzer' => 'ik_smart', | |
], | |
'desc' => [ | |
'type' => 'text', | |
'analyzer' => 'ik_max_word', | |
'search_analyzer' => 'ik_smart' | |
], | |
'created_at' => [ | |
'type' => 'date', | |
'format' => 'yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis' | |
] | |
], | |
]; | |
} |
我就简单的配置了一点,上面我用到中文分词,这里就不阐述怎么安装ik了,具体的根据自己的字段来选择合适的类型。然后在商品的模型:
namespace App\Models; | |
use App\Elasticsearch\IndexConfigurators\ProductIndexConfigurator; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
use ScoutElastic\Searchable; | |
class Product extends Model | |
{ | |
use HasFactory, | |
Searchable; | |
protected $indexConfigurator = ProductIndexConfigurator::class; | |
public function toSearchableArray() { | |
return [ | |
'id' => $this->id, | |
'title' => $this->title, | |
'desc' => $this->desc, | |
'created_at' => $this->created_at | |
] | |
} | |
} |
在模型中设置映射后,可以更新 Elasticsearch
类型映射,也就是把我们刚才创建的索引和商品的模型绑定在一起。
php artisan elastic:update-mapping "App\Models\Product"
你也可以把数据库的数据导入到Elasticsearch
中,具体的使用方法还是去看看这两个扩展的详细文档。
php artisan scout:import "App\Models\Product"
到这里基本都成了,嘿嘿。
四. 搜索使用
这个扩展的好处就是基本上的搜索还是和Laravel
中ORM
一致,会少一点,具体的还是自己去看文档
Product::search('phone') | |
// specify columns to select | |
->select(['title', 'price']) | |
// filter | |
->where('color', 'red') | |
// sort | |
->orderBy('price', 'asc') | |
// collapse by field | |
->collapse('brand') | |
// set offset | |
->from(0) | |
// set limit | |
->take(10) | |
// get results | |
->get(); |
结语
感谢一下扩展的优秀作者,才能让我们好好偷懒,感谢!
- scout-elasticsearch-driver
- laravel/scout