从 elasticsearch-php 6.x 到 8.x - 无法将响应反序列化为对象

问题描述 投票:0回答:1

我需要将网站 php 从 elasticsearch-php 6.x 更新到 8.9.1(当前)。 旧代码的示例

    /* init Elasticsearch-PHP */
    if (!is_object($this->client)) {
      $this->client = Elastic\Elasticsearch\ClientBuilder::create()
        ->setHosts(["127.0.0.1:9200"])
        ->setRetries(0)
        ->build();
    }
    /* get all indexes */
    try {
      $es_indexes = $this->client->cat()->indices();
    } catch (Exception $e) {
      $e = json_decode($e->getMessage(), true);
      return $e;
    }

$es_indexes 是一个包含所有信息的数组

[
    {
        "health": "yellow",
        "status": "open",
        "index": "indexname_1",
        "uuid": "xnBhWf0WQS-Ag-kBbpqiIw",
        "pri": "5",
        "rep": "1",
        "docs.count": "26388",
        "docs.deleted": "0",
        "store.size": "239.2mb",
        "pri.store.size": "239.2mb"
    },
   ...
]

但现在返回一个对象,如果我尝试反序列化

var_dump($es_indexes->asString());

结果是

string(936) "yellow open indexname_1    hJdFa9N6TAWwggUdURd6eg 1 1  22057 0 738.9mb 738.9mb
yellow open indexname_2    lXJPeOsyTBCvwTFjiyt27w 1 1  26389 1 222.4mb 222.4mb
yellow open indexname_3 HJevaEA0Rhuy25JhajwO4Q 1 1     67 0  96.4kb  96.4kb
...
"
var_dump($es_indexes->asObject());

结果是

Fatal error:  Uncaught Elastic\Transport\Exception\UnknownContentTypeException: Cannot deserialize the reponse as object with Content-Type: text/plain; charset=UTF-8 in /var/www/website/htdocs/vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:150
Stack trace:
#0 /var/www/website/htdocs/assets/libs/class.main.php(178): Elastic\Elasticsearch\Response\Elasticsearch->asObject()
#1 /var/www/website/htdocs/assets/libs/config.php(138): MAIN->getAllIndexes()
#2 /var/www/website/htdocs/index.php(2): require_once('...')
#3 {main}
  thrown in /var/www/website/htdocs/vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php on line 150

有什么想法吗?

porting elasticsearch-php
1个回答
0
投票

解决了更改请求的问题

    /* get all indexes */
    try {
      $es_indexes = $this->client->indices()->stats(['index']);
    } catch (Exception $e) {
      $e = json_decode($e->getMessage(), true);
      return $e;
    }

    foreach ($es_indexes->asArray()['indices'] as $index => $data) {
    ...
© www.soinside.com 2019 - 2024. All rights reserved.