elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.25k stars 964 forks source link

Cannot deserialize the reponse as object with Content-Type: text/plain #1354

Closed Sparviero-Sughero closed 10 months ago

Sparviero-Sughero commented 10 months ago

I need to update a website php from elasticsearch-php 6.x to 8.9.1 (current).

    /* 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 should be an object with all indexes

var_dump($es_indexes->asString());

output is

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
...
"

but if i use asObject or asArray

var_dump($es_indexes->asObject());

output is

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

System details

OS: Debian 12.1 PHP: 8.2 ES: 8.9.1

Composer info

elastic/transport v8.7.0 HTTP transport PHP library for Elastic products elasticsearch/elasticsearch v8.9.0 PHP Client for Elasticsearch guzzlehttp/guzzle 7.8.0 Guzzle is a PHP HTTP client library guzzlehttp/promises 2.0.1 Guzzle promises library guzzlehttp/psr7 2.6.1 PSR-7 message implementation that also provides common utility methods

Sparviero-Sughero commented 10 months ago

this is my solution

    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) {