diff --git a/config/elasticsearch.php b/config/elasticsearch.php index c331071..ce20dca 100644 --- a/config/elasticsearch.php +++ b/config/elasticsearch.php @@ -2,6 +2,8 @@ return [ 'host' => env('ELASTICSEARCH_HOST', 'localhost:9200'), + 'username' => env('ELASTICSEARCH_USERNAME'), + 'password' => env('ELASTICSEARCH_PASSWORD'), 'index' => env('ELASTICSEARCH_INDEX', 'index'), 'enabled' => env('ELASTICSEARCH_ENABLED', false), 'models' => [ diff --git a/src/LaravelElasticServiceProvider.php b/src/LaravelElasticServiceProvider.php index 66f4b12..af60847 100644 --- a/src/LaravelElasticServiceProvider.php +++ b/src/LaravelElasticServiceProvider.php @@ -35,7 +35,14 @@ public function configurePackage(Package $package): void public function packageRegistered(): void { $this->app->singleton(Client::class, function () { - return ClientBuilder::create()->setHosts([config('elasticsearch.host')])->build(); + $builder = ClientBuilder::create() + ->setHosts([config('elasticsearch.host')]); + + if (config('elasticsearch.username') && config('elasticsearch.password')) { + $builder->setBasicAuthentication(config('elasticsearch.username'), config('elasticsearch.password')); + } + + return $builder->build(); }); } }