Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of Elasticsearch index names using the Elasticsearch-PHP client?

I need a list of index names from Elasticsearch that match a certain pattern. Using Kibana I've no problem doing this, but I simply can't figure out how to do the same with the Elasticsearch-PHP client.

Example:

Trying to get indices matching the name pattern "*.foo.bar"
With Kibana: GET /_cat/indices/*.foo.bar

Does anyone know? I've found nothing on this in the Elasticsearch-PHP docs.

like image 902
Johan Fredrik Varen Avatar asked Apr 20 '18 11:04

Johan Fredrik Varen


2 Answers

I figured it out through trial and error.

The way to get a list of indices matching a pattern is:

$client = ClientBuilder::create()->build();
$indices = $client->cat()->indices(array('index' => '*.foo.bar'));
like image 138
Johan Fredrik Varen Avatar answered Oct 02 '22 12:10

Johan Fredrik Varen


In the current docs at the time of this response (7.2), you can find the documentation for the endpoint GET /_cat/indices/ that you're looking for.

So you can get the indices with this code:

$params = [
    // Example of another param
    'v' => true,
    // ...
    'index' => '*.foo.bar'
];

$indices = $client->cat()->indices($params);

The documentation doesn't explicitly states about the index param, but you can see how the index is set inside the CatNamespace::indices() method definition.

public function indices(array $params = [])
{
    $index = $this->extractArgument($params, 'index');
    ...
    $endpoint->setIndex($index);
    ...
}
like image 36
Rodrirokr Avatar answered Oct 02 '22 12:10

Rodrirokr