Elasticsearch-PHP库使用报错:No alive nodes found in your cluster[64] in ../Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Hyperf Elasticsearch-PHP库使用报错:No alive nodes found in your cluster[64] in ../Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php

前言

环境

项目

版本

PHP v8.0

Hyperf

v3.0
Elasticsearch v8.0
elasticsearch-php v7.17.1


服务地址

使用nginx将 服务器本地9200端口的elasticsearch服务映射到es.test.cn/es/

006.png

起因

代码

在hyperf中使用elasticsearch-php库连接elasticsearch服务,代码如下:

    #[GetMapping(path: "ela")]
    public function ela () {
        $host = "http://es.test.cn/es/";
        $builder = $this->container->get(ClientBuilderFactory::class)->create();
        $client = $builder
            ->setHosts([$host])
            ->setBasicAuthentication("username", "pwd")
            ->build();
        $info = $client->info();
        return $info;
    }

接口返回

002.png

服务端报错信息

[ERROR] No alive nodes found in your cluster[64] in /data/project/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php

000.png

分析

官方文档

按照elasticsearch官方的文档,host可以使用以下类型

  • IP + 端口
  • 仅IP
  • 域名 + 端口
  • 仅域名
  • Https 本地地址
  • Https IP + 端口

001.png

库源码

查看elasticsearch-php库源码,发现在解析host时,如果没有端口会默认加上9200端口,位置如下:

vendor\elasticsearch\elasticsearch\src\Elasticsearch\ClientBuilder.php

005.png

解决

代码

所以这个问题的解决就是在域名后加上相应nginx映射端口,我是默认80端口:es.test.cn:80,代码如下:

#[GetMapping(path: "ela")]
    public function ela () {
        $host = "http://es.test.cn:80/es/";
        $builder = $this->container->get(ClientBuilderFactory::class)->create();
        $client = $builder
            ->setHosts([$host])
            ->setBasicAuthentication("username", "pwd")
            ->build();
        $info = $client->info();
        return $info;
    }


接口返回

如下图,正确返回服务信息:

003.png

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
6天前
|
PHP
PHP——oneinstack重新安装PHP的时候报错
PHP——oneinstack重新安装PHP的时候报错
25 4
|
7天前
|
PHP
PHP——安装ThinkPHP框架报错
PHP——安装ThinkPHP框架报错
8 0
|
2月前
|
JSON 搜索推荐 大数据
Elasticsearch:从 ES|QL 到 PHP 对象
【6月更文挑战第9天】Elasticsearch 是一款强大的开源搜索引擎,适用于大数据处理和分析。在 PHP 开发中,使用 ES|QL 构建复杂查询后,通常需将查询结果转换为 PHP 对象。通过 `json_decode()` 函数解析 JSON 数据,可以实现这一目标。示例代码展示了如何将 Elasticsearch 响应转换为 PHP 对象并遍历数据。这样,我们可以进一步处理和操作数据,适应不同项目需求。随着技术和方法的更新,不断学习和适应将提升我们在开发中的效率和创新力。
64 10
|
2月前
|
PHP
php 使用phpize报错Cannot find config.m4. Make sure that you run ‘/usr/bin/phpize‘ in the top l
php 使用phpize报错Cannot find config.m4. Make sure that you run ‘/usr/bin/phpize‘ in the top l
125 1
|
2月前
|
Ubuntu PHP
ubuntu php libzip安装 ./configure报错 checking for libzip... not found configure
ubuntu php libzip安装 ./configure报错 checking for libzip... not found configure
55 1
|
2月前
|
PHP 数据库
phpMyAdmin报错 in ./libraries/config/FormDisplay.php#661 continue targeting switch is equivalent to
phpMyAdmin报错 in ./libraries/config/FormDisplay.php#661 continue targeting switch is equivalent to
23 0
|
3月前
|
Java 中间件 Serverless
Serverless 应用引擎操作报错合集之在阿里函数计算中,云函数怎么一直报错Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'php server.php '.如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
PHP
【PHP报错集锦】 Maximum function nesting level of ‘256‘ reached, aborting!
【PHP报错集锦】 Maximum function nesting level of ‘256‘ reached, aborting!
432 0
【PHP报错集锦】 Maximum function nesting level of ‘256‘ reached, aborting!
|
1月前
|
数据库
基于PHP+MYSQL开发制作的趣味测试网站源码
基于PHP+MYSQL开发制作的趣味测试网站源码。可在后台提前设置好缘分, 自己手动在数据库里修改数据,数据库里有就会优先查询数据库的信息, 没设置的话第一次查询缘分都是非常好的 95-99,第二次查就比较差 , 所以如果要你女朋友查询你的名字觉得很好 那就得是她第一反应是查和你的缘分, 如果查的是别人,那不好意思,第二个可能是你。
40 3
|
2月前
|
NoSQL 关系型数据库 MySQL
linux服务器重启php,nginx,redis,mysql命令
linux服务器重启php,nginx,redis,mysql命令
46 1