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

2

7

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.

Salesmanship answered 20/4, 2018 at 11:48 Comment(0)
S
7

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'));
Salesmanship answered 20/4, 2018 at 12:26 Comment(0)
A
1

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);
    ...
}
Approximal answered 12/11, 2019 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.