## 拉取组件 爱你项目的根目录require组件,执行: composer require elasticsearch/elasticsearch ~2.0 可以看到目前这个`PHP Client for Elasticsearch`组件的版本是`v5.3.0`,为什么我们选择`v2.0`的版本呢? 因为该项目中使用的Elasticsearch版本是`v2.3.2`,版本号大于`v1.0`并且小于`v5.0`,按照官方文档,选择`v2.0`相匹配Elasticsearch版本。 附packagist.org地址: https://packagist.org/packages/elasticsearch/elasticsearch ,有兴趣可以直接看英文文档。 ## 创建客户端实例 <?php use Elasticsearch\ClientBuilder; require 'vendor/autoload.php'; $client = ClientBuilder::create()->build(); ## 索引一行 代码: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', 'body' => [ 'martist_2017_field_1' => '123', 'martist_2017_field_2' => '456', 'martist_2017_field_3' => '789', ] ]; $response = $client->index($params); echo "<pre>"; print_r($response); 结果: Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 1 [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [created] => 1 ) es_head显示: ![](https://box.kancloud.cn/75b887cf642ffc8fac86f327d15ab424_2574x608.png) ## 检索一行数据 代码: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $response = $client->get($params); echo "<pre>"; print_r($response); 结果: Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 1 [found] => 1 [_source] => Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ) ## 检索(只要返回数组内键名为‘_source’的数据) 代码: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $source = $client->getSource($params); echo "<pre>"; print_r($source); 结果: Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ## 检索(按照字段) 代码: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'body' => [ 'query' => [ 'match' => [ 'martist_2017_field_1' => '123' ] ] ] ]; $response = $client->search($params); echo "<pre>"; print_r($response); 结果: Array ( [took] => 4 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 0.30685282 [hits] => Array ( [0] => Array ( [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_score] => 0.30685282 [_source] => Array ( [martist_2017_field_1] => 123 [martist_2017_field_2] => 456 [martist_2017_field_3] => 789 ) ) ) ) ) ## 删除(索引) 代码: $deleteParams = [ 'index' => 'martist_2017_index' ]; $response = $client->indices()->delete($deleteParams); print_r($response); 结果: Array ( [acknowledged] => 1 ) ## 删除(行) 代码: $params = [ 'index' => 'martist_2017_index', 'type' => 'martist_2017_type', 'id' => 'martist_2017_id', ]; $response = $client->delete($params); print_r($response); 结果: Array ( [found] => 1 [_index] => martist_2017_index [_type] => martist_2017_type [_id] => martist_2017_id [_version] => 2 [_shards] => Array ( [total] => 2 [successful]=> 1 [failed] => 0 ) ) 此时观察es-head ![](https://box.kancloud.cn/b5b74e9206abed75c46a9f912d1cab6e_1830x594.png) ## 创建索引 代码: $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); print_r($response); ## 结果: Array ( [acknowledged] => 1 ) ## 使用Mock进行单元测试 代码: use GuzzleHttp\Ring\Client\MockHandler; use Elasticsearch\ClientBuilder; // The connection class requires 'body' to be a file stream handle // Depending on what kind of request you do, you may need to set more values here $handler = new MockHandler([ 'status' => 200, 'transfer_stats' => [ 'total_time' => 100 ], 'body' => fopen('somefile.json') ]); $builder = ClientBuilder::create(); $builder->setHosts(['somehost']); $builder->setHandler($handler); $client = $builder->build(); // Do a request and you'll get back the 'body' response above