Elasticsearch-PHP 快速开始

快速开始

 

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

 

安装

 

 

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

 

 

 

[javascript] view plain copy
 
  1. {  
  2.     "require": {  
  3.         "elasticsearch/elasticsearch": "~1.0"  
  4.     }  
  5. }  

 

 

 

  • 使用composer安装客户端:

 

 

[plain] view plain copy
 
  1. curl -s http://getcomposer.org/installer | php  
  2. php composer.phar install  

 

 

  • 在主项目(一般是index.php)中引入autoloader.php文件(如果你还没有引入的话),并且要实例化Elasticsearch的客户端:

 

 

 

[php] view plain copy
 
  1. require 'vendor/autoload.php';  
  2.   
  3. $client = new Elasticsearch\Client();  

 

 

 

索引一个文档

 

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

 

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

 

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

 

[php] view plain copy
 
  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 plain copy
 
  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 plain copy
 
  1. $searchParams['index'] = 'my_index';  
  2. $searchParams['type']  = 'my_type';  
  3. $searchParams['body']['query']['match']['testField'] = 'abc';  
  4. $retDoc = $client->search($searchParams);  

 

 

删除一个文档

 

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

 

[php] view plain copy
 
  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 plain copy
 
  1. $deleteParams = array();  
  2. $deleteParams['index'] = 'my_index';  
  3. $client->indices()->delete($deleteParams);  

 

 

创建一个索引

 

 

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

 

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



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

 

index():

 

[plain] view plain copy
 
  1. create index success  

 

get():

 

[javascript] view plain copy
 
  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 plain copy
 
  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 plain copy
 
  1. {  
  2.     "found": true,  
  3.     "_index": "my_index",  
  4.     "_type": "my_type",  
  5.     "_id": "my_id",  
  6.     "_version": 2  
  7. }  

 

deleteIndex():

 

[javascript] view plain copy
 
  1. {  
  2.     "acknowledged": true  
  3. }  

 

createIndex():

 

[javascript] view plain copy
 
    1. {  
    2.     "acknowledged": true  
    3. }  
posted @ 2017-10-12 17:40  Nullnullisnull  阅读(2492)  评论(0编辑  收藏  举报