ElasticSearch安装
es的安装,参考macOs-安装ES这篇文章
安装laravel使用es的包,我的laravel版本5.8
composer require laravel/scout "^7.2"
- 注意:要指定版本,因为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
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
并在最后加入添加驱动
'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 "~v7.2"
composer require babenkoivan/scout-elasticsearch-driver "~4.2"
使用
创建 command 命令,生成ES结构
php artisan make:command ESInit
修改 app\Console\Command\ESInit.php文件
protected $signature = 'es:init';
protected $description = '初始化es索引';
在app\Console\Kernel.php中挂载
protected $commands = [
\App\Console\Commands\EsInit::class,
];
完成之后使用php artisan list 查看是否挂载成功
image.png
安装 guzzlehttp/guzzle 扩展
composer require guzzlehttp/guzzle
配置
修改 app\Console\Command\ESInit.php
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;
}
创建索引
image.pngphp artisan es:init
用已有数据库 向ES中插入“文档”
<?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 另一种写法
public function toSearchableArray()
{
return [
'company_name' => $this->company_name,
'history_name' => $this->history_name,
// 需要注意 时间字段 需要转义,否则会失败
'created_at' => Carbon::parse($this->created_at)->toDateTimeString(),
];
}
利用 postman 创建索引
导入无数据问题
- 初次使用有人就会问,我导入了没报错,为啥没数据,
- 因为没有创建索引,EsInit 好比是创建表,索引好比字段
- 那我对 es 又不了解怎么设置索引类型
- 请看文档,postman创建字段
- 所有都弄好了还是无数据
- env 文件配置如下
// 默认走的是队列
SCOUT_QUEUE=false
SCOUT_ELASTIC_DOCUMENT_REFRESH=true
网友评论