Laravel Scout 是針對(duì)Eloquent 模型開(kāi)發(fā)的一個(gè)簡(jiǎn)單的,基于驅(qū)動(dòng)的全文檢索系統(tǒng)。Scout 使用模型觀察者時(shí)會(huì)自動(dòng)保持你的檢索索引與你的 Eloquent 記錄同步。
目前,Scout 帶著一個(gè)Algolia驅(qū)動(dòng);然而,擴(kuò)展 Scout 并不難,你可以通過(guò)自定義驅(qū)動(dòng)來(lái)自由的擴(kuò)展 Scout。Scout的使用請(qǐng)看文檔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
主要參數(shù)說(shuō)明:
-d 后臺(tái)守護(hù)進(jìn)程
--name 為容器命名
-v 掛載文件,這里是把當(dāng)前宿主機(jī)的目錄 掛載到容器的項(xiàng)目目錄上
elasticsearch.yml 文件內(nèi)容
cluster.name: "docker-cluster"
network.host: 0.0.0.0
path.data: /data/es/data
http.cors.enabled: true
http.cors.allow-origin: "*"
瀏覽器訪問(wèn):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'),
],
],
創(chuàng)建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()
{
//創(chuàng)建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分詞插件,不然數(shù)據(jù)無(wú)法導(dǎo)入
"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("=======創(chuàng)建模板成功=======");
//創(chuàng)建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("=========創(chuàng)建索引成功=========");
}
}
model代碼
需要修改model里的代碼
/**
* App\Models\User
*
* @property int $id
* @property string $username 用戶(hù)名
* @property string $createtime 創(chuàng)建時(shí)間
*/
class User extends Model
{
use Searchable;
public function searchableAs()
{
// 對(duì)應(yīng)es里的_type,相當(dāng)于mysql表的概念
return 'users_index';
}
public function toSearchableArray()
{
$array = $this->toArray();
return $array;
}
}
導(dǎo)入數(shù)據(jù)
php artisan scout:import "\App\Models\User"

es表內(nèi)容
如果需要在Logstash導(dǎo)入es,可以參考下方地址
logstash 導(dǎo)入es配置文件,可參考 http://www.itdecent.cn/p/9b24df449851