B
B
BonBon Slick2017-04-26 13:22:28
Laravel
BonBon Slick, 2017-04-26 13:22:28

Unresolvable dependency resolving [Parameter #0 [ $retries ]] in class Elasticsearch\Transport?

Laravel 4.2 -> Created an ElasticServiceProvider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;

class ElasticServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(Elastic::class, function ($app) {
            return new Elastic(
                ClientBuilder::create()
                    ->setLogger(ClientBuilder::defaultLogger(storage_path('logs/elastic.log')))
                    ->build()
            );
        });

        $this->app->bind(Client::class, function () {
            return ClientBuilder::create()->build();
        });
    }

}

wrapper class:
namespace App\Elastic;

use Elasticsearch\Client;

class Elastic
{
    protected $client;
    public function __construct(Client $client)
    {
        $this->client = $client;
    }
    public function search(array $parameters)
    {
        return $this->client->search($parameters);
    }
}

Controller:
public function searchUser()
    {
        $elastic = app(App\Elastic\Elastic::class);

        dd($elastic);

Mistake:
lluminate \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $retries ]] in class Elasticsearch\Transport

Heh, I even tried to make it crooked, to blind everything directly in the controller, straight from the off-docs, and still the same error!
<?php

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application as Apy;

class HomeController extends BaseController
{
    protected $client;
    protected $app;

    public function __construct(Client $client, Apy $app)
    {
        $this->client = $client;
        $this->app = $app;
    }
    public function searchUser()
    {
        App::bind('foo', function($app)
        {
            return new User;
        });
        $value = App::make('foo');
        dd($value);

Also tried different versions of Elasticsearch 2/5...
How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2017-04-26
@BonBonSlick

There were a lot of bugs, one of which is that the Elasticsearch version should be ~1.0 for Laravel 4.2 .
+ you need to install JVM
+ install Elastic via console
+ composer.json file... classmap": [ .... , you need to add new folders with classes and enter composer dumpautoload (in my case)

"autoload": {
    "classmap": [
      "app/Observers",
      "app/Providers",
      "app/Services",

+ model observers to create something like this:
public function saved($model)
    {
        $this->client->index([
            'index' => 'lara_users',
            'type' => 'user',
            'id' => $model->id,
            'body' => $model->toArray()
        ]);
    }
// User class
 public static function boot()
    {
        parent::boot();
        $client = App::make(Elasticsearch\Client::class);
        self::observe(new App\Observers\UserObserver($client));
    }

Here, no third-party package is needed. Pure Elasticsearch, unless you still need a wrapper, here:
link to Git
+ If you missed something, Google & Toaster with the pros to help :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question