php - Elasticsearch PHP fatal error :未捕获的TypeError:参数1

标签 php elasticsearch

一般来说,我是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>

如果需要它,我将很乐意提供其他任何解决方案。

最佳答案

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

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

关于php - Elasticsearch PHP fatal error :未捕获的TypeError:参数1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229066/

相关文章:

php - 当通过 Controller 路由子 URL 时,Laravel 5.2 balde/css 停止工作

php - 无法从导航菜单栏的数据库中获取数据

Java 实体未映射到 ElasticSearch GeoPoint 属性

elasticsearch - Elasticsearch中的通配符搜索或部分匹配

php - 使用 JQuery 自动完成强制选择

javascript - 将 php 数组传递给 javascript 数组

php - PHP 并发

python - pip 安装不会向 pipFile 或 pipFile.lock 添加任何内容

java - 在 lucene 索引中存储和检索 Json 对象

ruby-on-rails - 如何使用Searchkick搜索当前用户的费用?