1.laravel-scout擴展包的安裝
composer require laravel/scout
會下載到laravel框架的vendor/laravle/scout目錄下
2.在config/app.php配置providers
數組中:添加scout服務提供者
Laravel\Scout\ScoutServiceProvider::class,
3.註冊好 Scout 的服務提供者之後,你可以使用 vendor:publish
Artisan 命令生成 Scout 的配置文件。這個命令會在你的 config
目錄下生成 scout.php
配置文件:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
這個命令的作用就是把/vendor/laravel/scout/config/scout.php配置文件複製到config目錄下,scout服務的配置就在這個配置文件中。
此時laravel-scout擴展包就算安裝好了。
2.給scout包適配elasticsearch引擎
我們一般使用elasticsearch作爲全文搜索引擎
2.1 安裝laravel-scout-elastic擴展包,這個就是操作elasticsearch的擴展包
composer require tamayo/laravel-scout-elastic
安裝包會下載到vendor/tamayo/laravel-scout-elastic目錄下
2.2在config/app.php的providers
數組中添加服務提供者
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
2.3修改scout配置文件,把驅動修改爲elasticsearch並且配置索引和es服務地址
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
//elasticsearch引擎的配置
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel54'),//這是索引名(相當於mysql的數據庫)
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),//這是es服務的地址
],
],
3.啓動elasticsearch服務可用
先確保elasticsearch服務可用,就是用瀏覽器訪問http://127.0.0.1:9200,如果出現下面,表示服務正常
{
"name" : "vhr6l8u",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "fIwrtOj4SUyHxO9ddul4_g",
"version" : {
"number" : "5.1.1",
"build_hash" : "5395e21",
"build_date" : "2016-12-06T12:36:15.409Z",
"build_snapshot" : false,
"lucene_version" : "6.3.0"
},
"tagline" : "You Know, for Search"
}
elasticsearch的restfulAPI
我們和es服務交互時通過restfulAPI,通過http協議,所以引入Guzzle這個PHP HTTP客戶端,他可以在php中直接發送各種的http請求
4.laravel的Guzzle包安裝使用
composer require guzzlehttp/guzzle
4.1常用的用法
$client = new Client;
$url = "http://192.168.1.207/icar_dev101/miniapp.php/sstore/hot_goods_list/";
//get通過普通參數獲取
$response = $client->get($url,['query' => ['sstore_id' => 1]]);
//post提交普通參數
$response = $client->post($url,['form_params'=>['id'=>2]]);
//post提交文件
$response = $client->post($url,['multipart'=>[
[
'name' => 'qux',//字段鍵名
'contents' => fopen('/path/to/file', 'r'),//文件句柄
'filename' => 'custom_filename.txt' //文件名
]
]]);
//put提交json
$response = $client->put($url, ['json' => ['foo' => 'bar']]);
//返回的是response對象,下面就是接口返回的數據
dd($response->getBody()->getContents());
5.使用elasticsearch
5.1創建索引,一般用mappings指定好映射
{
"mappings":{
"articles":{
"properties":{
"id":{
"type":"integer"
},
"title":{
"type":"text"
},
"content":{
"type":"text"
},
"user_id":{
"type":"integer"
},
"updated_at":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
把這個json字符用put方式提交給http://localhost:9200/blog,系統會創建blog索引,類型是article,字段就是id,title等。可以用postman提交創建
5.2使用laravel-commend創建索引
php artisan make:command ESInit
會創建app\Console\Command\ESInit.php文件 ,可以看到一個屬性protected $signature = 'es:init';則使用這個命令就用
php artisan es:init
//就會執行app\Console\Command\ESInit.php文件的handle方法
在app\Console\Kernel.php中掛載命令
protected $commands = [
\App\Console\Commands\ESInit::class,
];
修改編輯handle方法
public function handle()
{
$client = new Client;
//創建ES的索引
$url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index');
//$client->delete($url);//刪除這個索引
$param = [
"json"=>[
"mappings"=>[
"article"=>[
"properties"=>[
"title"=>[
"type"=>"keyword"
],
"content"=>[
"type"=>"text"
]
]
]
]
]
];
$client->put($url,$param);
$this->info("創建ES索引成功!");
}
使用命令 php artisan es:init 就會執行handle方法。根據配置創建一個索引,裏面類型是article,字段有title,content
5.3與對應的model關聯
創建一個articlemodel,然後修改articlemodel類
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
use Searchable;
// 定義索引裏面的type,相當於表
public function searchableAs(){
return 'article';
}
// 定義哪些字段需要搜索
public function toSearchableArray(){
return [
'title'=>$this->title,
'content'=>$this->content,
];
}
}
當我們的控制器使用App\Models\Article添加修改article表時,會對應修改到elasticsearch的索引blog,類型爲articles裏面的文檔。scout.php配置文件配置了elasticsearch綁定的index和host.相當於指定了數據庫。而類型則是在model裏面綁定。
6.window使用elasticsearch和head
https://www.cnblogs.com/gangle/p/9328257.html
6.1elasticsearch服務開啓
運行elasticsearch目錄下的bin下面的elasticsearch.bat,然後訪問http://127.0.0.1:9200
6.2head工具開啓
安裝好(需要安裝node.js和grunt),到/htdocs/elasticsearch-head-master目錄下,執行
grunt server
就會啓動head服務,訪問http://localhost:9100/即可