gpt4 book ai didi

php - Elasticsearch 配置在laravel v5.3中不起作用

转载 作者:行者123 更新时间:2023-12-03 01:41:05 25 4
gpt4 key购买 nike

我已经安装了新的laravel v5.3项目并安装了 Elasticsearch 驱动程序,以通过composer实现 Elasticsearch 。但是,当我重新加载页面时,我总是会收到此页面,即使 Elasticsearch 正在我的系统上运行,也无法正常工作。下面是我编写的完整代码。

composer.json

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

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

控制者
<?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;
}
}

但是,当我刷新页面后,此页面无法正常工作,我收到了此消息,并且页面未加载一件事,我想告诉您,我对app.php文件的配置未做任何更改。请教育以解决此问题。

最佳答案

如果要使用某些配置实例化 flex 客户端,则应使用ClientBuilder::fromConfig(array $config)方法。
在你的情况下应该是

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

如您所见,主机必须以数组形式提供。

另外,我不确定您使用的Elasticsearch客户端是否具有 ElasticClient类。

另外,如果您从 Controller 提供了实际代码,则它包含错误。您应该这样调用类属性: print_r($this->client)(属性名称附近没有 $)。

最后,您的 Controller 应如下所示:
<?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;
}
}

并将文档添加到索引中您需要根据官方文档调用此命令
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

官方文档可以在这里找到 https://github.com/elastic/elasticsearch-php

附言对不起我的英语不好。这远非完美。

关于php - Elasticsearch 配置在laravel v5.3中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com