一,添加别名
1,代码:
//初始化es的client
$client = $this->_init_es();
// 确定参数
$params = [
'index' => 'gs_second', //索引名字
'name' => 'gs_second_idx', //索引的别名
];
// 执行
//$client = EsHelper::getEsClient();
try {
$response = $client->indices()->putAlias($params);
$result = $response->asArray();
echo "响应:<br/>";
if ($result['acknowledged'] == true) {
echo "别名创建成功";
} else {
echo "别名创建失败";
}
var_dump($result);
}catch (\Exception $e){
$result = $e->getMessage();
echo "异常:<br/>";
var_dump($result);
}
2,测试效果:
添加前:
liuhongdi@lhdpc:~$ curl -sXGET localhost:9200/_cat/aliases?v
alias index filter routing.index routing.search is_write_index
gs_second_idx gs_second - - - -
添加后:
liuhongdi@lhdpc:~$ curl -sXGET localhost:9200/_cat/aliases?v
alias index filter routing.index routing.search is_write_index
gs_second_idx gs_second - - - -
二,删除别名
1,代码:
//初始化es的client
$client = $this->_init_es();
// 确定参数
$params = [
'index' => 'gs_second', //索引名字
'name' => 'gs_second_idx', //索引的别名
];
// 执行
//$client = EsHelper::getEsClient();
try {
$response = $client->indices()->deleteAlias($params);
$result = $response->asArray();
echo "响应:<br/>";
if ($result['acknowledged'] == true) {
echo "别名删除成功";
} else {
echo "别名删除失败";
}
var_dump($result);
}catch (\Exception $e){
$result = $e->getMessage();
echo "异常:<br/>";
var_dump($result);
}
2,测试效果:
删除前:
liuhongdi@lhdpc:~$ curl -sXGET localhost:9200/_cat/aliases?v
alias index filter routing.index routing.search is_write_index
gs_second_idx gs_second - - - -
删除后:
liuhongdi@lhdpc:~$ curl -sXGET localhost:9200/_cat/aliases?v
alias index filter routing.index routing.search is_write_index
三,根据别名得到索引名
1,代码:
$params = [
'name'=>'gs_second_idx', //要查询的别名
];
try {
$response = $client->indices()->getAlias($params);
$result = $response->asArray();
var_dump($result);
}catch (\Exception $e){
$result = $e->getMessage();
}
2,测试效果:
array(1) { ["gs_second"]=> array(1) { ["aliases"]=> array(1) { ["gs_second_idx"]=> array(0) { } } } }
四,根据索引名得到别名
1,代码:
$params = [
'index'=>'gs_second',
];
try {
$response = $client->indices()->getAlias($params);
$result = $response->asArray();
var_dump($result);
}catch (\Exception $e){
$result = $e->getMessage();
}
2,测试效果:
array(1) { ["gs_second"]=> array(1) { ["aliases"]=> array(1) { ["gs_second_idx"]=> array(0) { } } } }
说明:getAlias()方法不加参数则得到列出所有索引的别名
标签:index,gs,别名,second,client,elasticsearch,result,php From: https://www.cnblogs.com/architectforest/p/18348895