【问题标题】:How can I get a list of Elasticsearch index names using the Elasticsearch-PHP client?如何使用 Elasticsearch-PHP 客户端获取 Elasticsearch 索引名称列表?
【发布时间】:2018-10-01 03:09:20
【问题描述】:
我需要 Elasticsearch 中与特定模式匹配的索引名称列表。使用 Kibana 做这件事我没有问题,但我根本不知道如何用 Elasticsearch-PHP 客户端做同样的事情。
例子:
Trying to get indices matching the name pattern "*.foo.bar"
With Kibana: GET /_cat/indices/*.foo.bar
有人知道吗?我在 Elasticsearch-PHP 文档中对此一无所获。
【问题讨论】:
标签:
php
elasticsearch
elasticsearch-indices
【解决方案1】:
在此响应 (7.2) 时的 current docs 中,您可以找到您正在寻找的端点 GET /_cat/indices/ 的文档。
因此您可以使用以下代码获取索引:
$params = [
// Example of another param
'v' => true,
// ...
'index' => '*.foo.bar'
];
$indices = $client->cat()->indices($params);
文档没有明确说明index 参数,但您可以看到索引是如何在CatNamespace::indices() 方法定义中设置的。
public function indices(array $params = [])
{
$index = $this->extractArgument($params, 'index');
...
$endpoint->setIndex($index);
...
}
【解决方案2】:
我通过反复试验弄明白了。
获取匹配模式的索引列表的方法是:
$client = ClientBuilder::create()->build();
$indices = $client->cat()->indices(array('index' => '*.foo.bar'));