一,新增別名
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()方法不加引數則得到列出所有索引的別名