mailerlite / laravel-elasticsearch

An easy way to use the official Elastic Search client in your Laravel applications.
MIT License
909 stars 187 forks source link

Package doesn't detect .env file / environment variable #118

Closed iamalexly closed 2 years ago

iamalexly commented 3 years ago

Hi all,

I want to use this package with Laravel 8, so I've install the package and publish the configuration file. After that step, I've add the differents environment variables :

ELASTICSEARCH_HOST=elasticsearch
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=
ELASTICSEARCH_PASS=

I've created a Laravel command to test the package. Here, the code of the handle method of the command :

public function handle()
{
    $client = ClientBuilder::create()->build();
    dump($client->watcher());
}

And the dump response in the console :

#serializer: Elasticsearch\Serializers\SmartSerializer {#870 ā€¦}
#transportSchema: "http"
#host: "localhost"
#path: null
#port: 9200

So, my configuration is not set.

If I change the host directly when I create the ClientBuilder like :

public function handle()
{
    $client = ClientBuilder::create()->setHosts(['http://elasticsearch:9200'])->build();
    dump($client->watcher());
}

I've this response :

#serializer: Elasticsearch\Serializers\SmartSerializer {#870 ā€¦}
#transportSchema: "http"
#host: "elasticsearch"
#path: null
#port: 9200

So, if I don't change the host when I create the ClientBuilder, the package doesn't detect any nodes... but if I precise the host when I create the ClientBuilder, it's works !

Also, I've clear my cache and my config...

I don't understand, if anyone have an answer, I would gladly take it šŸ˜„

riouruma commented 3 years ago

I've got same issue.

iamalexly commented 3 years ago

I think I've now understand correctly this package ^^, but PHPStorm doesn't have a good plugin for Laravel, so it put me on the wrong way.

I think this message can help you @riouruma šŸ˜‰

In the config/app.php file, add the provider :

...
'providers' => [
    ...
    Cviebrock\LaravelElasticsearch\ServiceProvider::class,
    ...
],
...

Also, in the same file, add the aliases :

...
'aliases' => [
    ...
    'Elasticsearch' => Cviebrock\LaravelElasticsearch\Facade::class
    ...
],
...

Now, you can use the alias Elasticsearch into your commands/controllers...

use Elasticsearch;

And call all the methods in static :

$return = Elasticsearch::watcher();
dump($return);

Return of this two lines :

#serializer: Elasticsearch\Serializers\SmartSerializer {#400 ā€¦}
#transportSchema: "http"
#host: "elasticsearch"
#path: null
#port: "9200"

Now, my environment variables are used.

In fact, the ClientBuilder isn't a fonctionnality of this package but a fonctionnality of the Elastic PHP Package šŸ˜‰

In my opinion, the biggest problem is I'm using PHPStorm. He doesn't recognize the Elasticsearch namespace, so he doesn't autocomplete the methods available.

Also, if you use a tool like SonarQube, your code is considered as wrong because the namespace is not found...

cviebrock commented 2 years ago

Closing old tickets.