Elasticsearch-PHP 快速开始

本章节会给你一个客户端的主要功能(函数)是如何工作的快速概述。

安装

  • 引入(包含)elasticsearch-php 在你的 composer.json 文件:

[javascript]view plaincopy

  1. {
  2. "require": {
  3. "elasticsearch/elasticsearch": "~1.0"
  4. }
  5. }
  • 使用composer安装客户端:

[plain]view plaincopy

  1. curl -s http://getcomposer.org/installer | php
  2. php composer.phar install
  • 在主项目(一般是index.php)中引入autoloader.php文件(如果你还没有引入的话),并且要实例化Elasticsearch的客户端:

[php]view plaincopy

  1. require 'vendor/autoload.php';
  2. $client = new Elasticsearch\Client();

索引一个文档

在elasticsearch-php中,几乎所有的东西都是通过数组配置的。REST 的端点(终结点),文档和可选参数,一切都是一个关联数组。

去索引一个文档,我们简单的指定一个主体(body)来包含我们希望索引的文档。文档中的每一个字段都由一个关联数组的键/值对表示。

索引(index),类型(type)和 ID 也被指定在数组参数中,数组如下:

[php]view plaincopy

  1. $params = array();
  2. $params['body'] = array('testField' => 'abc');
  3. $params['index'] = 'my_index';
  4. $params['type'] = 'my_type';
  5. $params['id'] = 'my_id';
  6. $ret = $client->index($params);

获取一个文档

让我们来获取我们刚刚索引的文档:

[php]view plaincopy

  1. $getParams = array();
  2. $getParams['index'] = 'my_index';
  3. $getParams['type'] = 'my_type';
  4. $getParams['id'] = 'my_id';
  5. $retDoc = $client->get($getParams);

搜索一个文档

搜索是 elasticsearch 的一个标志,所以让我们执行搜索。我们打算使用匹配查询作为示范:

[php]view plaincopy

  1. $searchParams['index'] = 'my_index';
  2. $searchParams['type'] = 'my_type';
  3. $searchParams['body']['query']['match']['testField'] = 'abc';
  4. $retDoc = $client->search($searchParams);

删除一个文档

好的,让我们继续删除一个我们之前添加的文档:

[php]view plaincopy

  1. $deleteParams = array();
  2. $deleteParams['index'] = 'my_index';
  3. $deleteParams['type'] = 'my_type';
  4. $deleteParams['id'] = 'my_id';
  5. $retDelete = $client->delete($deleteParams);

删除一个索引

由于 elasticsearch 的动态性质,我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引,因为我们以后想要指定自己的设置:

[php]view plaincopy

  1. $deleteParams = array();
  2. $deleteParams['index'] = 'my_index';
  3. $client->indices()->delete($deleteParams);

创建一个索引

好吧,我们的索引被清空了,现在我们开始添加一个新的索引和一些自定义设置:

[php]view plaincopy

  1. $indexParams['index'] = 'my_index';
  2. $indexParams['body']['settings']['number_of_shards'] = 2;
  3. $indexParams['body']['settings']['number_of_replicas'] = 0;
  4. $client->indices()->create($indexParams);

总结

那些只是在客户端速成课程和语法上的概述。如果你熟悉elasticsearch, 你会注意到,这些方法的命名就像 REST 的端点(终结点)。

你还会发现客户端的配置方式使你发现通过你的IDE配置会非常方便。所有的核心操作都在 $client 对象(索引,搜索,获取等)下。索引和集群管理分别位于 $client->indices() 和 $client->cluster() 对象下。

查看剩下的文档去了解整个客户端是如何工作的。

例子代码

[php]view plaincopy

  1. <?php
  2. require 'vendor/autoload.php';
  3. $client = new Elasticsearch\Client();
  4. index($client);
  5. //get($client);
  6. // search($client);
  7. // deleteDoc($client);
  8. // deleteIndex($client);
  9. // createIndex($client);
  10. function index($client) {
  11. $params = array ();
  12. $params ['body'] = array (
  13. 'testField' => 'abc'
  14. );
  15. $params ['index'] = 'my_index';
  16. $params ['type'] = 'my_type';
  17. $params ['id'] = 'my_id';
  18. try {
  19. $ret = $client->index($params);
  20. println("create index success");
  21. } catch(Exception $e) {
  22. echo $e->getMessage();
  23. }
  24. }
  25. function get($client) {
  26. $getParams = array ();
  27. $getParams ['index'] = 'my_index';
  28. $getParams ['type'] = 'my_type';
  29. $getParams ['id'] = 'my_id';
  30. $retDoc = $client->get($getParams);
  31. println($retDoc);
  32. }
  33. function search($client) {
  34. $searchParams ['index'] = 'my_index';
  35. $searchParams ['type'] = 'my_type';
  36. $searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc';
  37. $retDoc = $client->search($searchParams);
  38. println($retDoc);
  39. }
  40. function deleteDoc($client) {
  41. $deleteParams = array ();
  42. $deleteParams ['index'] = 'my_index';
  43. $deleteParams ['type'] = 'my_type';
  44. $deleteParams ['id'] = 'my_id';
  45. $retDelete = $client->delete($deleteParams);
  46. println($retDelete);
  47. }
  48. function deleteIndex($client) {
  49. $deleteParams = array ();
  50. $deleteParams ['index'] = 'my_index';
  51. $retDelete = $client->indices()->delete($deleteParams);
  52. println($retDelete);
  53. }
  54. function createIndex($client) {
  55. $indexParams ['index'] = 'my_index';
  56. $indexParams ['body'] ['settings'] ['number_of_shards'] = 2;
  57. $indexParams ['body'] ['settings'] ['number_of_replicas'] = 0;
  58. $retCreate = $client->indices()->create($indexParams);
  59. println($retCreate);
  60. }
  61. function println($var) {
  62. echo "<br>";
  63. $type = gettype($var);
  64. if ($type == "array" || $type == "object") {
  65. echo json_encode($var);
  66. } else {
  67. echo $var;
  68. }
  69. echo "<br>";
  70. }

查看每个方法的运行结果:

index():

[plain]view plaincopy

  1. create index success

get():

[javascript]view plaincopy

  1. {
  2. "_index": "my_index",
  3. "_type": "my_type",
  4. "_id": "my_id",
  5. "_version": 1,
  6. "found": true,
  7. "_source": {
  8. "testField": "abc"
  9. }
  10. }

search():

[javascript]view plaincopy

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 1,
  11. "max_score": 0.30685282,
  12. "hits": [
  13. {
  14. "_index": "my_index",
  15. "_type": "my_type",
  16. "_id": "my_id",
  17. "_score": 0.30685282,
  18. "_source": {
  19. "testField": "abc"
  20. }
  21. }
  22. ]
  23. }
  24. }

deleteDoc():

[javascript]view plaincopy

  1. {
  2. "found": true,
  3. "_index": "my_index",
  4. "_type": "my_type",
  5. "_id": "my_id",
  6. "_version": 2
  7. }

deleteIndex():

[javascript]view plaincopy

  1. {
  2. "acknowledged": true
  3. }

createIndex():

[javascript]view plaincopy

    1. {
    2. "acknowledged": true
    3. }