Elasticsearch PHP致命错误:未捕获的TypeError:参数1

I'm new to Elasticsearch and PHP in general. I've been trying to figure out a problem with a code I've recieved. I'm usng WAMP 3.1 with PHP 7.1 and Elasticsearch 6.2.

When I go to my localhost I recieve the following error:

Fatal error: Uncaught TypeError: Argument 1 passed to Elasticsearch\Client::__construct() must be an instance of Elasticsearch\Transport, array given, called in C:\wamp64\www\search\init.php on line 5 and defined in C:\wamp64\www\search\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Client.php:98 Stack trace: #0 C:\wamp64\www\search\init.php(5): Elasticsearch\Client->__construct(Array) #1 C:\wamp64\www\search\index.php(2): require_once('C:\\wamp64\\www\\s...') #2 {main} thrown in C:\wamp64\www\search\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Client.php on line 98

I can see that there is probably a problem with my Init.php but I've been using the one I recieved without modification so I don't know for sure if thats the problem.

    require_once 'vendor/autoload.php';
    $es = new Elasticsearch\Client([
        'hosts' => ['']

Here is also the index.php.

require_once 'init.php';
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = Elasticsearch\ClientBuilder::create()->build();

    if (isset($_GET['q'])) {
        $q = $_GET['q'];
        $query = $es->search([
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            'match' => ['name' => $q],
                            'match' => ['content' => $q]
    echo '<pre>', print_r($query), '</pre>';

    if($query['hits']['total'] >=1 ) {
        $results = $query['hits']['hits'];

    <!doctype html>
            <meta charset="utf-8">
            <title>search | ES</title>

            <link rel="stylesheet" href="css/main.css">
            <form action="index.php" method="get" autocomplete="off">
                    <input type="text" name="q">

                <input type="submit" value="Search">

            if(isset($results)) {
                foreach($results as $r) {
                <div class="result">
                    <a href="#<?php echo $r['_id']; ?>"><?php echo $r['_source']['title'];?></a>
                    <div class="result-keywords"><?php implode(', ', $r['_source']['keywords']);?></div>

I would be happy to provide anything else if its neccecary to solve the problem.


你根本不需要 init </ code>(除了你在那里使用错误的客户端类) 。 只需这样做:</ p>

  require_once'ports / autoload.php'; 
use Elasticsearch \ ClientBuilder;
$ client = ClientBuilder :: create() - &gt; setHosts([''])->build();

</ div>



You don't need init at all (besides you're using the client class wrong in there). Just do:

require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts([''])->build();

Csdn user default icon