Elasticsearch-PHP 命名空间

客户端有很多命名空间,通常能够暴漏出他管理的功能。命名空间对应Elasticsearch各种管理的端点。如下是完成的命名空间的列表:

命名空间功能
indices()以指数为中心的统计数据和信息
nodes()以节点为中心的统计数据和信息
cluster()以集群为中心的统计数据和信息
snapshot()快照/还原您的集群和指示器的方法
cat()访问Cat API(这是通常用于的独立从命令行访问)

有些方法可以在不同的命名空间下使用,给你相同的信息,但是分组到不同的上下文。如果要查看这些命名空间是如何工作的,让我们来看看_stats 的输出:

  1. $client = new Elasticsearch\Client();
  2. // Index Stats
  3. // Corresponds to curl -XGET localhost:9200/_stats
  4. $response = $client->indices()->stats();
  5. // Node Stats
  6. // Corresponds to curl -XGET localhost:9200/_nodes/stats
  7. $response = $client->nodes()->stats();
  8. // Cluster Stats
  9. // Corresponds to curl -XGET localhost:9200/_cluster/stats
  10. $response = $client->cluster()->stats();

正如你所看到的,同样的 stats() 调用在三个不同的命名空间。有时候方法需要参数。这些参数的工作原理就像其它库中的方法一样。

例如我们可以请求一个特定的索引或多个索引信息的统计。

  1. $client = new Elasticsearch\Client();
  2. // Corresponds to curl -XGET localhost:9200/my_index/_stats
  3. $params['index'] = 'my_index';
  4. $response = $client->indices()->stats($params);
  5. // Corresponds to curl -XGET localhost:9200/my_index1,my_index2/_stats
  6. $params['index'] = array('my_index1', 'my_index2');
  7. $response = $client->indices()->stats($params);

另一个例子,如何在现有索引中添加一个别名。

  1. $params['body'] = array(
  2. 'actions' => array(
  3. array(
  4. 'add' => array(
  5. 'index' => 'myindex',
  6. 'alias' => 'myalias'
  7. )
  8. )
  9. )
  10. );
  11. $client->indices()->updateAliases($params);

请注意 stats 调用和 updateAlias 是如何接受一个多样化的参数,每一个都是根据特定API的需要。stats API仅仅需要一个索引名称,然而 updateAlias 需要一个body上的动作。