Laravel Scout 是针对Eloquent 模型开发的一个简单的,基于驱动的全文检索系统。Scout 使用模型观察者时会自动保持你的检索索引与你的 Eloquent 记录同步。
目前,Scout 带着一个Algolia驱动;然而,扩展 Scout 并不难,你可以通过自定义驱动来自由的扩展 Scout。Scout的使用请看文档Scout 全文搜索
docker安装elasticsearch
docker run -d --name es -v /Users/chen/data/docker/es:/data/es -v /Users/chen/data/docker/es/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.8.5
主要参数说明:
-d 后台守护进程
--name 为容器命名
-v 挂载文件,这里是把当前宿主机的目录 挂载到容器的项目目录上
elasticsearch.yml 文件内容
cluster.name: "docker-cluster"
network.host: 0.0.0.0
path.data: /data/es/data
http.cors.enabled: true
http.cors.allow-origin: "*"
浏览器访问:http://127.0.0.1:9200 就能看到es版本信息
Scout配置文件
//config/scout.php文件中添加下面代码
//这里是添加的代码
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel1'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
],
创建es索引
php artisan make:command ESInitUser
<?php
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class ESInitUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init:user';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//创建template
$client = new Client();
$url = config('scout.elasticsearch.hosts')[0] . '/_template/users';
//$client->delete($url);
$param = [
'json' => [
'template' => config('scout.elasticsearch.index'),
'mappings' => [
'_default_' => [
'dynamic_templates' => [
[
'analyzer_fileds' => [
'match' => 'username',
'match_mapping_type' => 'string',
"mapping" => [
"type" => "text",
"norms" => false,
"analyzer" => "ik_max_word",//elasticsearch 要安装 ik分词插件,不然数据无法导入
"search_analyzer" => "ik_smart"
]
]
],
[
'date_fileds' => [
'match' => 'createtime',
'match_mapping_type' => 'string',
"mapping" => [
"type" => "date",
"format" => "yyyy-MM-dd HH:mm:ss"
]
]
]
]
]
],
],
];
$client->put($url, $param);
//记录
$this->info("=======创建模板成功=======");
//创建index
$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
$client->delete($url);
$param = [
'json' => [
'settings' => [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
];
$client->put($url, $param);
//记录
$this->info("=========创建索引成功=========");
}
}
model代码
需要修改model里的代码
/**
* App\Models\User
*
* @property int $id
* @property string $username 用户名
* @property string $createtime 创建时间
*/
class User extends Model
{
use Searchable;
public function searchableAs()
{
// 对应es里的_type,相当于mysql表的概念
return 'users_index';
}
public function toSearchableArray()
{
$array = $this->toArray();
return $array;
}
}
导入数据
php artisan scout:import "\App\Models\User"
如果需要在Logstash导入es,可以参考下方地址
logstash 导入es配置文件,可参考 https://www.jianshu.com/p/9b24df449851