doushi9856 2018-05-08 08:18
浏览 242
已采纳

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.

    <?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>

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

  • 写回答

1条回答 默认 最新

  • dongxindu8753 2018-05-08 08:22
    关注

    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(['127.0.0.1:9200'])->build();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题