安裝Scout
(1)首先,使用 composer 包管理器來安裝 Scout,如果沒有安裝 composer 包管理器,要先安裝;接著進(jìn)入laravel項(xiàng)目的根目錄使用composer 命令安裝
```
composer require laravel/scout
```
(2)接下來,你需要將 ScoutServiceProvider 添加到你的 config/app.php 配置文件的 providers 數(shù)組中:
Laravel\Scout\ScoutServiceProvider::class,
(3)注冊(cè)好 Scout 的服務(wù)提供者之后,你可以使用 vendor:publish Artisan 命令生成 Scout 的配置文件。這個(gè)命令會(huì)在你的 config 目錄下生成 scout.php 配置文件:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
(4)使用 composer安裝scout的es驅(qū)動(dòng):
composerrequiretamayo/laravel-scout-elastic
(5)安裝完驅(qū)動(dòng)之后,修改config\scout.php配合文件,將驅(qū)動(dòng)修改為elasticsearch
'driver'=> env('SCOUT_DRIVER','elasticsearch'),
并在下方添加驅(qū)動(dòng):
'elasticsearch'=> [//laravel54是項(xiàng)目名,可以自定義'index'=> env('ELASTICSEARCH_INDEX','laravel'),'hosts'=> [? ? ? ? ? ? env('ELASTICSEARCH_HOST','http://127.0.0.1:9200'),? ? ? ? ],? ? ],
創(chuàng)建command命令
(1)使用php artisan創(chuàng)建command命令
php artisan make:commandESInit
(2)執(zhí)行完命令后會(huì)創(chuàng)建app\Console\Command\ESInit.php文件,修改ESInit.php
//使用什么命令啟動(dòng)腳本protected$signature ='es:init';//描述protected$description ='init laravel es for post';
(3)在app\Console\Kernel.php中掛載
protected $commands = [? ? ? ? \App\Console\Commands\ESInit::class];
完成之后使用php artisan命令查看命令是否掛載成功
安裝guzzlehttp/guzzle 擴(kuò)展
composerrequireguzzlehttp/guzzle
配置
(1)修改app\Console\Command\ESInit.php
```public function handle()
{
? ? $client=new Client();
? ? $url=config('scout.elasticsearch.hosts')[0]. '/_template/tmp';
? ? //$client->delete($url);
? ? $param = [
? ? ? ? 'json'=>[
? ? ? ? ? ? 'template' => config('scout.elasticsearch.index'),
? ? ? ? ? ? 'mappings' => [
? ? ? ? ? ? ? ? '_default_' => [
? ? ? ? ? ? ? ? ? ? 'dynamic_templates' => [
[
? ? ? ? ? ? ? ? ? ? ? ? ? ? 'strings' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'match_mapping_type' => 'string',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'mapping' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'type' => 'text',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'analyzer' => 'ik_smart',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'fields' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'keyword' => [
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'type' => 'keyword'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ]
]
]
]
]
]
]
? ? ? ? ? ? ],
? ? ? ? ],
? ? ];
? ? $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)建索引成功=========");
}
(2)在我的項(xiàng)目中我使用文章數(shù)據(jù)表來搜索,因此需要修改post.php,也就是posts數(shù)據(jù)對(duì)于的數(shù)據(jù)模型
useSearchable;//定義索引里面的type
public function searchableAs(){
return"post";? ??
}
//定義有哪些字段需要搜索
public function toSearchableArray()
{
$position_type=DB::table("class_name")->where('id',$this->position_type)->first();
$education=DB::table('config')->where('id',$this->education)->first();
return [
? ? 'position'=>$this->position,
? ? 'description'=>$this->description,
? ? 'province'=>$this->province,
? ? 'position_type'=>@$position_type->name,
? ? 'salary'=>$this->min_salary."k-".$this->salary."k",
? ? 'education'=>@$education->name,
];
? }
導(dǎo)入數(shù)據(jù)
使用php artisan命令導(dǎo)入數(shù)據(jù)
php artisan scout:import"\App\Position"
導(dǎo)入成功之后我們?cè)跒g覽器地址輸欄入:127.0.0.1:9200/laravel/position/23(laravel是elasticsearch驅(qū)動(dòng)定義的項(xiàng)目名,position對(duì)象的是我項(xiàng)目的position數(shù)據(jù)模型,23是某條數(shù)據(jù)的ID )