php elasticsearch 获取索引所有文档_Elasticsearch客户端主要方法的使用规则
安裝
1.在 composer.json 文件中引入 elasticsearch-php:
{ "require": { "elasticsearch/elasticsearch": "~6.0" }}2.用 composer 安裝客戶端:
curl -s http://getcomposer.org/installer | phpphp composer.phar install --no-dev3.在項目中引入自動加載文件(如果還沒引入),并且實例化一個客戶端:
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() 中。
來源:https://www.cnblogs.com/mzhaox/p/11·END·
PHP開源社區進階·提升·漲薪總結
以上是生活随笔為你收集整理的php elasticsearch 获取索引所有文档_Elasticsearch客户端主要方法的使用规则的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理财通5万一天多少利息?最全的理财通各类
- 下一篇: php date 报错,编译安装PHP报