ElasticSearch安装
es的安装,参考macOs-安装ES这篇文章
安装laravel使用es的包
composer require laravel/scout "^5.0.3"
- 注意:要指定版本,因为es驱动不支持scout高版本
php artisan vendor:publish--provider="Laravel\Scout\ScoutServiceProvider"
- 会生成一个config/scout.php配置文件
安装Scout的es驱动
laravel-scout-elastic | packagist 地址
laravel-scout-elastic | git 地址
composer require tamayo/laravel-scout-elastic
修改config\scout.php配合文件,将驱动修改为elasticsearch
1 | 'driver' => env('SCOUT_DRIVER', 'elasticsearch'), |
并在最后加入添加驱动
1 2 3 4 5 6 7 | 'elasticsearch' => [ 'index' => env('ELASTICSEARCH_INDEX', 'laravel_es'), //索引名,自定义 'hosts' => [ env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'), ], ], |
如果要使用babenkoivan/scout-elasticsearch-driver 请注意版本
composer require laravel/scout "~v3.0"
composer require tamayo/laravel-scout-elastic "~3.0"
composer require babenkoivan/scout-elasticsearch-driver "~2.4"
使用
- Laravel参考
创建 command 命令,生成ES结构
php artisan make:command ESInit
修改 app\Console\Command\ESInit.php文件
1 2 | protected $signature = 'es:init'; protected $description = '初始化es索引'; |
在app\Console\Kernel.php中挂载
1 2 3 | protected $commands = [ \App\Console\Commands\EsInit::class, ]; |
完成之后使用php artisan list 查看是否挂载成功
image.png
安装 guzzlehttp/guzzle 扩展
composer require guzzlehttp/guzzle
配置
修改 app\Console\Command\ESInit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | public function handle() { $client = new Client(); $url = config('scout.elasticsearch.hosts')[0] . '/_template/laravel_tmp_1'; $client->put($url, [ 'json' => [ 'index_patterns' => config('scout.elasticsearch.index'), 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 0, ], 'mappings' => [ '_doc' => [ '_source' => [ 'enabled' => true, ], //具体设置字段 'properties' => [ 'created_at' => [ 'type' => 'date', 'format' => 'yy-MM-dd HH:mm:ss||yy-MM-dd||epoch_millis', ], ], 'dynamic_templates' => [ [ 'strings' => [ 'match_mapping_type' => 'string', 'mapping' => [ 'type' => 'text', 'analyzer' => 'ik_smart', 'fields' => [ 'keyword' => [ 'type' => 'keyword', 'ignore_above' => 256, ], ], ], ], ], ], ], ], ], ]); $this->info('创建模板成功'); $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index'); $client->put($url, [ 'json' => [ 'settings' => [ 'refresh_interval' => '5s', 'number_of_shards' => 1, 'number_of_replicas' => 0, ], ], ]); $this->info('创建索引成功'); return true; } |
创建索引
php artisan es:init
image.png
用已有数据库 向ES中插入“文档”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace App\Models; use Carbon\Carbon; use Laravel\Scout\Searchable; use Illuminate\Database\Eloquent\Model; class Company extends Model { use Searchable; //定义索引里面的type. 在创建EsInit时已经创建好了 public function searchableAs() { return '_doc'; } // 存到ES有哪些字段 public function toSearchableArray() { return $this->toArray(); } } |
使用命令导入数据
php artisan scout:import "\App\Models\Company"
toSearchableArray 另一种写法
1 2 3 4 5 6 7 8 9 | public function toSearchableArray() { return [ 'company_name' => $this->company_name, 'history_name' => $this->history_name, // 需要注意 时间字段 需要转义,否则会失败 'created_at' => Carbon::parse($this->created_at)->toDateTimeString(), ]; } |
利用 postman 创建索引
ES的基础使用
导入无数据问题
- 初次使用有人就会问,我导入了没报错,为啥没数据,
- 因为没有创建索引,EsInit 好比是创建表,索引好比字段
- 那我对 es 又不了解怎么设置索引类型
- 请看文档,postman创建字段
- 所有都弄好了还是无数据
- env 文件配置如下
1 2 3 | // 默认走的是队列 SCOUT_QUEUE=false SCOUT_ELASTIC_DOCUMENT_REFRESH=true |
- IK分词 https://low.bi/p/NJD6x1jgRQE