1、composer安裝elasticsearch擴充包
composer require elasticsearch/elasticsearch "7.12.x" --ignore-platform-reqs
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLsR2VkVDZtJGao1WZwhmMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL3QjM2QjMwcTM0ITNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、配置es
config/database.php
'elasticsearch'=>[
'hosts' => explode(',',env('ES_HOSTS')),
]
配置.env
ES_HOSTS=192.168.84.3
3、初始化elasticsearch對象
注入到容器中 App/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Elasticsearch\ClientBuilder as ESClientBuilder;
class AppServiceProvider extends ServiceProvider{
/**
* Register any application services.
*
* @return void
*/
public function register(){
$this->app->singleton('es',function(){
$bulider = ESClientBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
if(app()->environment()==='local'){
$bulider->setLogger(app('log')->driver());
}
return $bulider->build();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
4、測試
php artisan tinker
>>>app('es')->info()
出現以上界面說明安裝成功了
如果出現 Elasticsearch\Common\Exceptions\NoNodesAvailableException with message 'No alive nodes found in your cluster'
最好确認下ES是否開啟,或者ES配置是否正确。
下一篇寫 Elasticsearch+kibana及分詞相關介紹。