dongwenyou4298 2017-11-23 09:51
浏览 87

弹性搜索配置在laravel v5.3中不起作用

I have setup new laravel v5.3 project and install elastic search driver to implement elastic search via composer. But when I reload my page then I always receive This page isn’t working even the elastic search is running on my system below is my complete code that I code.

composer.json

"require": {
        "php": ">=5.6.4",
        "elasticsearch/elasticsearch": "^6.0",
        "laravel/framework": "5.3.*"
    },

web.php

Route::get('/',array('uses' => 'ElasticSearch@addPeopleList'));

Controller

<?php
namespace App\Http\Controllers;    
class ElasticSearch extends Controller
    {

        // elastic
        protected $elastic;
        //elastic cliend
        protected $client;

        public function __construct(Client $client)
        {
            $this->client = ClientBuilder::create()->build();
            $config = [
                'host' =>'localhost',
                'port' =>9200,
                'index' =>'people',
            ];
            $this->elastic = new ElasticClient($config);
        }

        public function addPeopleList(){
            echo "<pre>";
            print_r($this->$elastic);
            exit;
        }
    }

But when I refresh the page then This page isn’t working i received this message and page not loaded one thing that I want to let you know that I made no changes in app.php file of configuration. Please eduacate to solve this issue.

  • 写回答

1条回答 默认 最新

  • dongpo2014 2017-11-23 15:08
    关注

    if You want to instantiate an elastic client with some configuration, You should use method ClientBuilder::fromConfig(array $config). In your case it should be

    <?php
    $client = ClientBuilder::fromConfig([
        'hosts' => [ 'localhost:9200' ]
    ]);
    

    As You can notice above hosts must be provided as array.

    Also I'm not sure that Elasticsearch client that You use have ElasticClient class.

    Also if You provided actual code from your controller than it contains an error. You should call class properties like that: print_r($this->client) (without $ near the property name).

    Finaly your controller should looks like this:

    <?php
    namespace App\Http\Controllers;
    
    use Elasticsearch\ClientBuilder;
    
    class ElasticSearch extends Controller
    {
        /**
         * @var \Elasticsearch\Client
         */
        protected $client;
    
        public function __construct()
        {
            $this->client = ClientBuilder::fromConfig([
                'hosts' => [
                    'localhost:9200',
                ],
            ]);
        }
        public function addPeopleList(){
            echo "<pre>";
            print_r($this->client);
            exit;
        }
    }
    

    And to add a document to the index You need to call this command according to the official documentation

    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id',
        'body' => ['testField' => 'abc']
    ];
    
    $response = $client->index($params);
    print_r($response);
    

    Official documentation can be found here https://github.com/elastic/elasticsearch-php

    P.S. Sorry for my English. It is far from perfect.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器