from elasticsearch-php 6.x to 8.x - fail to deserialize the reponse as object

75 Views Asked by At

I need to update a website php from elasticsearch-php 6.x to 8.9.1 (current). An example of old code

    /* 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 was an array with all infos

[
    {
        "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"
    },
   ...
]

but now return an object and if i try to deserialize

var_dump($es_indexes->asString());

the result 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
...
"
var_dump($es_indexes->asObject());

the result 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

any ideas?

1

There are 1 best solutions below

0
Suvier On

solved changing the request

    /* 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) {
    ...