Elasticsearch的安装参考:https://www.jianshu.com/p/f886b2c2fb8a
1 composer安装扩展包
composer require elasticsearch/elasticsearch "7.12.x"
一些环境依赖
requires
php:7.3||8.0
ext-json:>=1.3.7
ezimuel/ringphp:^1.1.2
psr/log:~1.0
2 相关配置
1.config/database.php配置
···
···
···
'elasticsearch' => [
'hosts' => explode(',',env("ES_HOSTS"))
],
···
···
···
2.配置.env文件
#es容器或者虚拟机(服务器)ip
ES_HOSTS=172.17.0.7
3.在AppServiceProvider.php注册一个单例
namespace App\Providers;
use Elasticsearch\ClientBuilder as ESClientBuilder;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//在laravel的容器中注册一个es的单例
$this->app->singleton('es',function (){
$builder = ESClientBuilder::create()->setHosts(config('database.elasticsearch.hosts'));
if (app()->environment() === 'local'){
$builder->setLogger(app('log')->driver());
}
return $builder->build();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
4.通过php artisan thinker命令行查看Elasticsearch的相关信息