一、安裝JDK
使用elasticsearch的前提是你的主機(jī)必須安裝了java的JDK,而且版本必須是1.8以上。安裝JDK,并設(shè)置環(huán)境變量。
下載地址:
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

二、下載elasticsearch安裝包;
下載地址:
https://github.com/medcl/elasticsearch-rtf
三、解壓安裝包
進(jìn)去安裝包bin目錄雙擊elasticsearch.bat
出現(xiàn)以下畫(huà)面:表運(yùn)行成功:在地址欄可以通過(guò)下圖圈內(nèi)地址訪問(wèn),查看是否安裝成功127.0.0.1:9200

出現(xiàn)如下圖,表安裝成功:

四、安裝Scout
(1)首先,使用 composer 包管理器來(lái)安裝 Scout,如果沒(méi)有安裝 composer 包管理器,要先安裝;接著進(jìn)入laravel項(xiàng)目的根目錄使用composer 命令安裝:
composer require laravel/scout
(2)接下來(lái),你需要將 ScoutServiceProvider 添加到你的 config/app.php 配置文件的 providers 數(shù)組中:(Laravel5.7不需進(jìn)行此操作);
DsdLaravel\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):
composer require tamayo/laravel-scout-elastic
如出現(xiàn)版本問(wèn)題(可以選擇下面這條命令)
composer require laravel/scout ^5.0.3
(5)安裝完驅(qū)動(dòng)之后,修改config\scout.php配合文件,將驅(qū)動(dòng)修改為elasticsearch
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
并在下方添加驅(qū)動(dòng):
'elasticsearch' => [
//laravel57是項(xiàng)目名,可以自定義
'index' => env('ELASTICSEARCH_INDEX', 'laravel57'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
],
五、創(chuàng)建command命令
(1)使用php artisan創(chuàng)建command命令
php artisan make:command ESInit
(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
];
(4)完成之后使用php artisan命令查看命令是否掛載成功
六、配置
(1)修改app\Console\Command\ESInit.php
在上方引入
use GuzzleHttp\Client;
public function handle()
{
//創(chuàng)建template
$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)在命令行中 使用 php artisan es:init 命令查看是否創(chuàng)建成功
(3)在我的項(xiàng)目中我使用文章數(shù)據(jù)表來(lái)搜索,因此需要修改模型文件,也就是post數(shù)據(jù)對(duì)于的數(shù)據(jù)模型加入如下代碼
use Searchable;
//定義索引里面的type
public function searchableAs()
{
return "post";
}
//定義有哪些字段需要搜索
public function toSearchableArray()
{
return [
'title'=>$this->title,
'content'=>$this->content,
];
}
七、導(dǎo)入數(shù)據(jù)
使用php artisan命令導(dǎo)入數(shù)據(jù)(命名空間加模型名)
php artisan scout:import "\App\Post"
出現(xiàn)下面表示成功


之后在控制器模型中可以通過(guò)search()方法獲取數(shù)據(jù)
例子:
App\Post::search(‘查詢的數(shù)據(jù)’)->get();