2

Following the elasticsearch documentation I installed the current php library ie 2.0 and I did this

$hosts = [
  // This is effectively equal to: "https://username:password!#$?*[email protected]:9200/"
  [
    'host' => 'foo.com',
    'port' => '9200',
    'scheme' => 'https',
    'user' => 'username',
    'password' => 'password!#$?*abc'
  ],

  // This is equal to "http://localhost:9200/"
  [
    'host' => 'localhost',    // Only host is required
  ]
];

$client = ClientBuilder::create()  // Instantiate a new ClientBuilder
              ->setHosts($hosts)   // Set the hosts
              ->build();

But it is throwing array to string conversion error from the buildConnectionsFromHosts method. I am unable to establish connection.

I inspected the code and found that there is no code to process host given in array form.Is this the bug in the library or I am missing something?

Thank you.

3
  • This is the configuration example from the documentation provided in the elasticsearch website. Please have a look at this .elastic.co/guide/en/elasticsearch/client/php-api/current/… Commented Sep 22, 2016 at 2:31
  • If you were actually following the guide, it looks to me you are opening an https connection when you don't have one enabled for your host. If this is the actual configuration you are using locally, then it won't work, no matter what, since you won't be able to connect to an elasticsearch on foo.com Commented Sep 22, 2016 at 11:10
  • @Bjorn I am aware of that fact and this is sample configuration I copied as I cannot show my actual configuration here. At least this should not throw array to string conversion error because of that. Commented Sep 22, 2016 at 12:37

1 Answer 1

3

The solution

The "password" key in the host options should be replaced with "pass".

The ClientBuilder.php file in the library needs to be modified. The below code is not there in elasticsearch-php 2.0 ClientBuilder.php file but is in its master branch.

I replaced buildConnectionsFromHosts method

/**
         * @param array $hosts
         *
         * @throws \InvalidArgumentException
         * @return \Elasticsearch\Connections\Connection[]
         */
        private function buildConnectionsFromHosts($hosts)
        {
            if (is_array($hosts) === false) {
                $this->logger->error("Hosts parameter must be an array of strings, or an array of Connection hashes.");
                throw new InvalidArgumentException('Hosts parameter must be an array of strings, or an array of Connection hashes.');
            }

            $connections = [];
            foreach ($hosts as $host) {
                if (is_string($host)) {
                    $host = $this->prependMissingScheme($host);
                    $host = $this->extractURIParts($host);
                } else if (is_array($host)) {
                    $host = $this->normalizeExtendedHost($host);
                } else {
                    $this->logger->error("Could not parse host: ".print_r($host, true));
                    throw new RuntimeException("Could not parse host: ".print_r($host, true));
                }
                $connections[] = $this->connectionFactory->create($host);
            }

            return $connections;
        }

and added normalizeExtendedHost method

 /**
     * @param $host
     * @return array
     */
    private function normalizeExtendedHost($host) {
        if (isset($host['host']) === false) {
            $this->logger->error("Required 'host' was not defined in extended format: ".print_r($host, true));
            throw new RuntimeException("Required 'host' was not defined in extended format: ".print_r($host, true));
        }

        if (isset($host['scheme']) === false) {
            $host['scheme'] = 'http';
        }
        if (isset($host['port']) === false) {
            $host['port'] = '9200';
        }
        return $host;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.