快速開始
本章節會給你一個客戶端的主要功能(函數)是如何工作的快速概述。
安裝
- 引入(包含)elasticsearch-php 在你的 composer.json 文件:
- {
- "require": {
- "elasticsearch/elasticsearch": "~1.0"
- }
- }
- 使用composer安裝客戶端:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install
- 在主項目(一般是index.php)中引入autoloader.php文件(如果你還沒有引入的話),並且要實例化Elasticsearch的客戶端:
- require 'vendor/autoload.php';
- $client = new Elasticsearch\Client();
索引一個文檔
在elasticsearch-php中,幾乎所有的東西都是通過數組配置的。REST 的端點(終結點),文檔和可選參數,一切都是一個關聯數組。
去索引一個文檔,我們簡單的指定一個主體(body)來包含我們希望索引的文檔。文檔中的每一個字段都由一個關聯數組的鍵/值對表示。
索引(index),類型(type)和 ID 也被指定在數組參數中,數組如下:
- $params = array();
- $params['body'] = array('testField' => 'abc');
- $params['index'] = 'my_index';
- $params['type'] = 'my_type';
- $params['id'] = 'my_id';
- $ret = $client->index($params);
獲取一個文檔
讓我們來獲取我們剛剛索引的文檔:
- $getParams = array();
- $getParams['index'] = 'my_index';
- $getParams['type'] = 'my_type';
- $getParams['id'] = 'my_id';
- $retDoc = $client->get($getParams);
搜索一個文檔
搜索是 elasticsearch 的一個標志,所以讓我們執行搜索。我們打算使用匹配查詢作為示范:
- $searchParams['index'] = 'my_index';
- $searchParams['type'] = 'my_type';
- $searchParams['body']['query']['match']['testField'] = 'abc';
- $retDoc = $client->search($searchParams);
刪除一個文檔
好的,讓我們繼續刪除一個我們之前添加的文檔:
- $deleteParams = array();
- $deleteParams['index'] = 'my_index';
- $deleteParams['type'] = 'my_type';
- $deleteParams['id'] = 'my_id';
- $retDelete = $client->delete($deleteParams);
刪除一個索引
由於 elasticsearch 的動態性質,我們添加第一個文檔的時候自動創建了索引和一些默認設置。讓我們刪除這個索引,因為我們以后想要指定自己的設置:
- $deleteParams = array();
- $deleteParams['index'] = 'my_index';
- $client->indices()->delete($deleteParams);
創建一個索引
好吧,我們的索引被清空了,現在我們開始添加一個新的索引和一些自定義設置:
- $indexParams['index'] = 'my_index';
- $indexParams['body']['settings']['number_of_shards'] = 2;
- $indexParams['body']['settings']['number_of_replicas'] = 0;
- $client->indices()->create($indexParams);
總結
那些只是在客戶端速成課程和語法上的概述。如果你熟悉elasticsearch, 你會注意到,這些方法的命名就像 REST 的端點(終結點)。
你還會發現客戶端的配置方式使你發現通過你的IDE配置會非常方便。所有的核心操作都在 $client 對象(索引,搜索,獲取等)下。索引和集群管理分別位於 $client->indices() 和 $client->cluster() 對象下。
查看剩下的文檔去了解整個客戶端是如何工作的。
例子代碼
- <?php
- require 'vendor/autoload.php';
- $client = new Elasticsearch\Client();
- index($client);
- //get($client);
- // search($client);
- // deleteDoc($client);
- // deleteIndex($client);
- // createIndex($client);
- function index($client) {
- $params = array ();
- $params ['body'] = array (
- 'testField' => 'abc'
- );
- $params ['index'] = 'my_index';
- $params ['type'] = 'my_type';
- $params ['id'] = 'my_id';
- try {
- $ret = $client->index($params);
- println("create index success");
- } catch(Exception $e) {
- echo $e->getMessage();
- }
- }
- function get($client) {
- $getParams = array ();
- $getParams ['index'] = 'my_index';
- $getParams ['type'] = 'my_type';
- $getParams ['id'] = 'my_id';
- $retDoc = $client->get($getParams);
- println($retDoc);
- }
- function search($client) {
- $searchParams ['index'] = 'my_index';
- $searchParams ['type'] = 'my_type';
- $searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc';
- $retDoc = $client->search($searchParams);
- println($retDoc);
- }
- function deleteDoc($client) {
- $deleteParams = array ();
- $deleteParams ['index'] = 'my_index';
- $deleteParams ['type'] = 'my_type';
- $deleteParams ['id'] = 'my_id';
- $retDelete = $client->delete($deleteParams);
- println($retDelete);
- }
- function deleteIndex($client) {
- $deleteParams = array ();
- $deleteParams ['index'] = 'my_index';
- $retDelete = $client->indices()->delete($deleteParams);
- println($retDelete);
- }
- function createIndex($client) {
- $indexParams ['index'] = 'my_index';
- $indexParams ['body'] ['settings'] ['number_of_shards'] = 2;
- $indexParams ['body'] ['settings'] ['number_of_replicas'] = 0;
- $retCreate = $client->indices()->create($indexParams);
- println($retCreate);
- }
- function println($var) {
- echo "<br>";
- $type = gettype($var);
- if ($type == "array" || $type == "object") {
- echo json_encode($var);
- } else {
- echo $var;
- }
- echo "<br>";
- }
查看每個方法的運行結果:
index():
- create index success
get():
- {
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_version": 1,
- "found": true,
- "_source": {
- "testField": "abc"
- }
- }
search():
- {
- "took": 3,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 0.30685282,
- "hits": [
- {
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_score": 0.30685282,
- "_source": {
- "testField": "abc"
- }
- }
- ]
- }
- }
deleteDoc():
- {
- "found": true,
- "_index": "my_index",
- "_type": "my_type",
- "_id": "my_id",
- "_version": 2
- }
deleteIndex():
- {
- "acknowledged": true
- }
createIndex():
- {
- "acknowledged": true
- }
