官網網址:https://github.com/medcl/elasticsearch-rtf
windows環境下,進入安裝目錄。點選elasticsearch.bat即可。如圖既安裝成功
注意:elasticsearch 依賴jdk環境,具體環境自己網上安裝檢視,這裡不詳述
安裝scout
composer require laravel/scout
安裝elssticsearch
composer require tamayo/laravel-scout-elastic
安裝GuzzleHttp包:
composer require Guzzlehttp/guzzle
在config/app.php的providers 陣列新增:
Laravel\Scout\ScoutServiceProvider::class,
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
釋出配置檔案:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
釋出生成的config/scout.php檔案新增
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),//索引名稱
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
],
php artisan make:command EsInit
具體實現程式碼如下:
namespace App\Console\Commands;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
class ESinit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init';
/**
* The console command description.
*
* @var string
*/
protected $description = 'init laravel';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new Client();
$this->createTemplate($client);
$this->createIndex($client);
}
//建立索引
protected function createIndex($client)
{
$url = config('scout.elasticsearch.hosts')[0].'/'.config('scout.elasticsearch.index');
$client->put($url, [
'json' => [
'settings' => [
'refresh_interval' => '5s',
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
]);
}
//建立模板
protected function createTemplate($client)
{
$url = config('scout.elasticsearch.hosts')[0] .'/_template/rtf';
$client->put($url, [
'json' => [
'template' => '*',
'settings' => [
'number_of_shards' => 1
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => true
],
'dynamic_templates' => [
[
'strings' => [
'match_mapping_type' => 'string',
'mapping' => [
'type' => 'text',
'analyzer' => 'ik_smart',
'ignore_above' => 256,
'fields' => [
'keyword' => [
'type' => 'keyword'
]
]
]
]
]
]
]
]
]
]);
}
}
初始化指令碼
php artisan es:init
namespace App\Models;
use Laravel\Scout\Searchable;
class Article extends BaseModel
{
use Searchable;
protected $guarded = [];
protected $table = 'article';
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content
];
}
}
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function search(Request $request)
{
$params=$request->keyword;
$article= Article::search($params)->paginate();
return $this->output($article, '請求成功', STATUS_OK);
}
}
php artisan scout:import "App\Models\Article"
命令完成會有如下提示,同時es裡面也會將資料庫指定欄位寫入
注:es視覺化工具自行百度安裝。
Algolia是法國初創公司為你提供毫秒級的資料庫實時搜尋服務。在這裡我也配置實驗了下,具體沒有壓測,有興趣的朋友可以嘗試下,這裡只寫簡單的使用。具體elsaticsearch和algolia如何選擇,看業務需求具體來選擇
在專案env檔案新增
SCOUT_DRIVER=algolia
SCOUT_PREFIX=
ALGOLIA_APP_ID=XXXXXXX
ALGOLIA_SECRET=XXXXXXXXXXX
這裡的appid和secret需要自己註冊獲取
官網網址:https://www.algolia.com
註冊成功後;
然後執行命令
php artisan scout:import "App\Models\Article"
執行完會發現資料已經寫到後臺了
然後按照之前配置好多路由去訪問
ElasticSearch作為一款強大的開源分散式搜尋與資料分析引擎,可快速從海量資料總找到相關資訊,近年來搜尋DBRanKing一直位列第一。還被廣泛應用於大資料近實時分析,包括日誌分析,指標監控,資訊保安等等,國內很多公司都在使用,騰訊,滴滴,小米,餓了麼,今日頭條等。所以這裡面要學的東西還是很多很多啊,具體資料如何分片,索引如何劃分,map設定,analysis具體細節,最佳資料模型構建,叢集擴充套件,引數配置,效能調優,等等等,都是我們需要去掌握的,前路漫漫其修遠兮啊,加油!