【问题标题】:Elasticsearch PHP Fatal error: Uncaught TypeError: Argument 1Elasticsearch PHP 致命错误:未捕获的类型错误:参数 1
【发布时间】:2018-05-08 08:18:03
【问题描述】:

总的来说,我是 Elasticsearch 和 PHP 的新手。我一直试图找出我收到的代码的问题。我将 WAMP 3.1 与 PHP 7.1 和 Elasticsearch 6.2 一起使用。

当我转到本地主机时,我收到以下错误:

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

我可以看到我的 Init.php 可能有问题,但我一直在使用我收到的未修改的,所以我不确定是否是问题。

    <?php 
    require_once 'vendor/autoload.php';
    $es = new Elasticsearch\Client([
        'hosts' => ['127.0.0.1:9200']
    ]);

Here is also the index.php.

<?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>
    <html>
        <head>
            <meta charset="utf-8">
            <title>search | ES</title>

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

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

            <?php
            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>
                </div>
            <?php 
            }
        }
         ?>
        </body>
    </html>

如果需要解决问题,我很乐意提供其他任何东西。

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    你根本不需要init(除了你在那里使用错误的客户端类)。做吧:

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

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 2022-12-10
      • 2021-06-14
      • 2023-03-25
      • 1970-01-01
      • 2021-07-08
      • 2020-07-28
      • 1970-01-01
      • 2017-04-02
      相关资源
      最近更新 更多