一、安裝環(huán)境
1诫硕、elasticsearch需要java環(huán)境雕蔽。所以先在java官網(wǎng)上下載java來安裝java環(huán)境
2、下載elasticsearch在java環(huán)境中的運(yùn)行包讳苦,該包實(shí)現(xiàn)搜索的索引等
下載地址:
https://github.com/medcl/elasticsearch-rtf
運(yùn)行:
Mac/Linux:
cd elasticsearch/bin
./elasticsearch -d
sudo -u ops ES_JAVA_OPTS="-Xms2024m -Xmx2024m" ./bin/elasticsearch -d
Windows:
cd elasticsearch/bin
elasticsearch.bat
插件比較多姓蜂,不用的插件可以刪除
留下analysis-ik插件即可
是否啟動(dòng)可在logs/elasticsearch.log中查看啟動(dòng)的ip端口,默認(rèn)9200和9300
或者在瀏覽器127.0.0.1:9200出現(xiàn)json可以說明安裝成功
三医吊、安裝laravel使用的elasticsearch所需要的包
1、laravel搜索驅(qū)動(dòng)(可以隨時(shí)更換驅(qū)動(dòng))
可以根據(jù)laravel的文檔安裝修改
composer require laravel/scout
你需要將 ScoutServiceProvider 添加到你的 config/app.php 配置文件的 providers 數(shù)組中:
Laravel\Scout\ScoutServiceProvider::class,
注冊好 Scout 的服務(wù)提供者之后逮京,你可以使用 vendor:publish Artisan 命令生成 Scout 的配置文件卿堂。這個(gè)命令會在你的 config 目錄下生成 scout.php 配置文件:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
2、安裝Laravel Scout Elasticsearch Driver
https://github.com/ErickTamayo/laravel-scout-elastic
下載:composer require tamayo/laravel-scout-elastic
增加:
'providers' => [
...
Laravel\Scout\ScoutServiceProvider::class,
...
ScoutEngines\Elasticsearch\ElasticsearchProvider::class,
],
四懒棉、修改配置scout.php
<?php
return [
'driver' => env('SCOUT_DRIVER', 'elasticsearch'),
'queue' => env('SCOUT_QUEUE', false),
'chunk' => [
'searchable' => 500,
'unsearchable' => 500,
],
'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
],
//增加
'elasticsearch' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
//這個(gè)laravel代表elasticsearch的索引草描,相當(dāng)于你的數(shù)據(jù)庫
'hosts' => [
env('ELASTICSEARCH_HOST', 'http://127.0.0.1:9200'),
],
//host后面是ip:port
],
];
五、Laravel自定義命令行
初始化ES
php artisan make:command ESint
安裝GuzzleHttp
在Console中Commands中編寫Esint
composer require guzzlehttp/guzzle
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
class ESInit extends Command
{
//php artisan make:command ESInit
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:init';
/**
* The console command description.
*
* @var string
*/
protected $description = 'init es';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new Client();
// 創(chuàng)建模版策严,進(jìn)行文本分析
$url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
// $client->delete($url);
$client->put($url, [
'json' => [
'template' => config('scout.elasticsearch.index'),
'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'
]
]
]
]
]
]
]
]
]
]);
$this->info('#############創(chuàng)建模板成功#############');
$url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
// $client->delete($url);
$client->put($url, [
'json' => [
'settings' => [
'refresh_interval' => '5s',//更新時(shí)間
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
'mappings' => [
'_default_' => [
'_all' => [
'enabled' => false
]
]
]
]
]);
$this->info('#############創(chuàng)建索引成功#############');
}
}
Kernel 中注冊
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//掛載新的命令
\App\Console\Commands\ESInit::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
六穗慕、導(dǎo)入數(shù)據(jù)
在你需要搜索的模型里面加入
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Article extends Model
{
//需要引入
use Searchable;
/**
* 得到該模型索引的名稱。相當(dāng)于表
*
* @return string
*/
public function searchableAs()
{
return 'article';
}
//定義哪些字段需要索引
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
];
}
}
導(dǎo)入數(shù)據(jù):
php artisan scout:import "帶上命名空間的模型名"
可以在瀏覽器輸入127.0.0.1:9200/laravel/article/id來查看
七妻导、渲染搜索頁面
在search方法中
$query就是搜索的字段逛绵,如:
$query = request('query');
$articles = Article::search($query)->paginate(6);