Elasticsearch-PHP 中文文档 Namespaces

caleb · 2021-05-20 12:47:35 · 热度: 11

命名空间

客户端有很多「命名空间」,通常是一些公开可管理的功能。命名空间对应着 Elasticseach 中各种可管理的 endpoints。下面是全部的命名空间:

命名空间 功能
indices() 索引数据统计和显示索引信息
nodes() 节点数据统计和显示节点信息
cluster() 集群数据统计和显示集群信息
snapshot() 对集群和索引进行产生快照或恢复数据
cat() 执行 Cat API (通常在命令使用)

一些方法在不同的命名空间都可以使用,虽然返回相同的信息,但是却在不同的上下文环境。想知道命名空间如何运作的,请看 _stats 输出信息:

$client = ClientBuilder::create()->build();

// 索引统计
// 对应于 curl -XGET localhost:9200/_stats
$response = $client->indices()->stats();

// 节点统计
// 对应于 curl -XGET localhost:9200/_nodes/stats
$response = $client->nodes()->stats();

// 集群统计
// 对应于 -XGET localhost:9200/_cluster/stats
$response = $client->cluster()->stats();

正如你所看到的,在三种不同的命名空间中都调用了相同的 stats() 方法。有些方法需要参数,这些参数写法与其他库中的函数写法相同。

例如,我们可以请求单个索引或者多个索引的统计信息:

$client = ClientBuilder::create()->build();

// Corresponds to curl -XGET localhost:9200/my_index/_stats
$params['index'] = 'my_index';
$response = $client->indices()->stats($params);

// Corresponds to curl -XGET localhost:9200/my_index1,my_index2/_stats
$params['index'] = array('my_index1', 'my_index2');
$response = $client->indices()->stats($params);

我们也可以给现有的索引添加别名:

$params['body'] = array(
    'actions' => array(
        array(
            'add' => array(
                'index' => 'myindex',
                'alias' => 'myalias'
            )
        )
    )
);
$client->indices()->updateAliases($params);

请遵循 API 来合理的调用 stats 和 updateAlias 的各种参数。 stats API 只需要一个索引名称, updateAlias 需要一个完整的行为。

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册