php: 操作elasticsearch的别名

一,添加别名

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()方法不加参数则得到列出所有索引的别名

五,判断别名是否存在

        //初始化es的client
        $client = $this->_init_es();
        $alias = 'gs_second_idx1';
        // 确定参数
        $params = [
            'name' => $alias,   //索引的别名
        ];

        $response = $client->indices()->existsAlias($params);
        //var_dump($response);
        $statusCode = $response->getStatusCode();
        if ($statusCode == 200) {
            echo "别名".$alias."存在<br/>";
        } else {
            echo "别名".$alias."不存在<br/>";
        }

 

posted @ 2024-08-08 14:59  刘宏缔的架构森林  阅读(18)  评论(0编辑  收藏  举报