1.在 composer.json 文件中引入 elasticsearch-php:
{ "require": { "elasticsearch/elasticsearch": "~6.0" } }
2.用 composer 安裝客戶端:
curl -s http://getcomposer.org/installer | php php composer.phar install --no-dev
3.在項目中引入自動加載文件(如果還沒引入),並且實例化一個客戶端:
require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; $client = ClientBuilder::create()->build();
索引一個文檔
在 elasticsearch-php 中,幾乎一切操作都是用關聯數組來配置。REST 路徑(endpoint)、文檔和可選參數都是用關聯數組來配置。
為了索引一個文檔,我們要指定4部分信息:index,type,id 和一個 body。構建一個鍵值對的關聯數組就可以完成上面的內容。body 的鍵值對格式與文檔的數據保持一致性。(譯者注:如 ["testField" ⇒ "abc"] 在文檔中則為 {"testField" : "abc"}):
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => ['testField' => 'abc'] ]; $response = $client->index($params); print_r($response);
收到的響應數據表明,你指定的索引中已經創建好了文檔。響應數據是一個關聯數組,里面的內容是 Elasticsearch 返回的decoded JSON 數據:
Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 1 [result] => created [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [_seq_no] => 0 [_primary_term] => 1 )
現在獲取剛才索引的文檔:
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->get($params); print_r($response);
響應數據包含一些元數據(如 index,type 等)和 _source
屬性, 這是你發送給 Elasticsearch 的原始文檔數據。
Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 1 [found] => 1 [_source] => Array ( [testField] => abc ) )
搜索是 elasticsearch 的一大特色,所以我們試一下執行一個搜索。我們准備用 Match 查詢來作為示范:
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $response = $client->search($params); print_r($response);
這個響應數據與前面例子的響應數據有所不同。這里有一些元數據(如 took
, timed_out
等)和一個 hits
的數組,這代表了你的搜索結果。而 hits
內部也有一個 hits
數組,內部的 hits
包含特定的搜索結果:
Array ( [took] => 16 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 0.2876821 [hits] => Array ( [0] => Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_score] => 0.2876821 [_source] => Array ( [testField] => abc ) ) ) ) )
好了,現在我們看一下如何把之前添加的文檔刪除掉:
$params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id' ]; $response = $client->delete($params); print_r($response);
你會注意到刪除文檔的語法與獲取文檔的語法是一樣的。唯一不同的是 delete
方法替代了 get
方法。下面響應數據代表文檔已被刪除:
Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 2 [result] => deleted [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [_seq_no] => 1 [_primary_term] => 1 )
由於 elasticsearch 的動態特性,我們創建的第一個文檔會自動創建一個索引,同時也會把 settings 里面的參數設定為默認參數。由於我們在后面要指定特定的 settings,所以現在要刪除掉這個索引:
$deleteParams = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response);
響應數據是:
Array ( [acknowledged] => 1 )
由於數據已被清空,我們可以重新開始了,現在要添加一個索引,同時要進行自定義 settings:
$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); print_r($response);
Elasticsearch會創建一個索引,並配置你指定的參數值,然后返回一個消息確認:
Array ( [acknowledged] => 1 [shards_acknowledged] => 1 [index] => my_index )
這里只是概述了一下客戶端以及它的語法。如果你很熟悉 elasticsearch,你會注意到這些方法的命名跟 REST 路徑(endpoint)是一樣的。
你也注意到了客戶端的參數配置從某種程度上講也是方便你的IDE易於搜索。$client 對象下的所有核心方法(索引,搜索,獲取等)都是可用的。索引管理和集群管理分別在 $client->indices()
和 $client->cluster()
中。