【问题标题】:How to find Index by Alias in Elasticsearch php client api如何在 Elasticsearch php 客户端 api 中按别名查找索引
【发布时间】:2016-12-30 05:06:38
【问题描述】:
我正在创建搜索应用程序。当我将数据重新索引到弹性搜索中时,重新索引时不应该停机。我想以零停机时间进行重新索引过程。我正在尝试这样做:
使用别名查找旧索引。
创建新索引并填充新数据
删除别名并删除旧索引
给新的索引别名
我们如何使用 php 客户端库来做到这一点。
【问题讨论】:
标签:
php
search
elasticsearch
【解决方案1】:
我不明白为什么人们对他投了反对票,问题很直接,而且弹性搜索的文档不容易理解!
总之,解决办法是这样的:
class SomeClass
{
/** @var \Elasticsearch\Client */
private $client;
/**
* @param \Elasticsearch\Client $client
*/
public function __construct(\Elasticsearch\Client $client)
{
$this->client = $client;
}
/**
* @param string $aliasName
*
* @return null|string
*/
public function findIndexNameByAlias($aliasName)
{
$aliases = $this->client->indices()->getAliases();
foreach ($aliases as $index => $aliasMapping) {
if (array_key_exists($aliasName, $aliasMapping['aliases'])) {
return $index;
}
}
return null;
}
}
$someClass = new SomeClass(new \Elasticsearch\Client());
echo "Index associated with 'MyAlias': " . $someClass->findIndexNameByAlias('MyAlias');