<?phpnamespace backend\controllers;use yii\web\Controller;/** * Site controller */class EsController extends Controller{ public function actionIndex() { $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build(); // 新增 // $demo1 = $this->demo1($client); // 查询 // $demo2 = $this->demo2($client); // 筛选 // $demo3 = $this->demo3($client); // 删除 // $demo4 = $this->demo4($client); // 删除索引 // $demo5 = $this->demo5($client); // 创建索引 $demo6 = $this->demo6($client); header("content-type: text/html;charset=utf-8"); echo "<pre>"; print_r($demo6); exit; } /** * 索引一个文档 * * 在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置。REST 路径(endpoint)、文档和可选参数都是用关联数组来配置。 * 为了索引一个文档,我们要指定4部分信息:index,type,id 和一个 body。 * 构建一个键值对的关联数组就可以完成上面的内容。body 的键值对格式与文档的数据保持一致性。 * (译者注:如 ["testField" ⇒ "abc"] 在文档中则为 {"testField" : "abc"}): * * https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html * @return 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 ) */ public function demo1($client) { $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', 'body' => [ 'testField' => 'abc' ] ]; $response = $client->index($params); return $response; } /** * 现在获取刚才索引(demo1)的文档: * * @param [type] $client * * 响应数据包含一些元数据(如 index,type 等)和 _source 属性, 这是你发送给 Elasticsearch 的原始文档数据。 * @return Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 3 [_seq_no] => 2 [_primary_term] => 1 [found] => 1 [_source] => Array ( [testField] => abc ) ) */ public function demo2($client) { $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', ]; $response = $client->get($params); return $response; } /** * 搜索一个文档edit * 搜索是 elasticsearch 的一大特色,所以我们试一下执行一个搜索。我们准备用 Match 查询来作为示范: * * 这个响应数据与前面例子的响应数据有所不同。这里有一些元数据(如 took, timed_out 等)和一个 hits 的数组, * 这代表了你的搜索结果。而 hits 内部也有一个 hits 数组,内部的 hits 包含特定的搜索结果: * @param [type] $client * @return Array ( [took] => 1 [timed_out] => [_shards] => Array ( [total] => 1 [successful] => 1 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => Array ( [value] => 1 [relation] => eq ) [max_score] => 0.18232156 [hits] => Array ( [0] => Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_score] => 0.18232156 [_source] => Array ( [testField] => abc ) ) ) ) ) */ public function demo3($client) { $params = [ 'index' => 'my_index', 'type' => 'my_type', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ], ]; $response = $client->search($params); return $response; } /** * 删除一个文档edit * 好了,现在我们看一下如何把之前添加的文档删除掉: * * @param [type] $client * * 你会注意到删除文档的语法与获取文档的语法是一样的。唯一不同的是 delete 方法替代了 get 方法。下面响应数据代表文档已被删除: * @return Array ( [_index] => my_index [_type] => my_type [_id] => my_id [_version] => 4 [result] => deleted [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [_seq_no] => 3 [_primary_term] => 1 ) * 删除后再次删除 * * Elasticsearch\Common\Exceptions\Missing404Exception: * {"_index":"my_index","_type":"my_type","_id":"my_id","_version":5,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":4,"_primary_term":1} */ public function demo4($client) { $params = [ 'index' => 'my_index', 'type' => 'my_type', 'id' => 'my_id', ]; $response = $client->delete($params); return $response; } /** * 删除一个索引edit * 由于 elasticsearch 的动态特性,我们创建的第一个文档会自动创建一个索引, * 同时也会把 settings 里面的参数设定为默认参数。由于我们在后面要指定特定的 settings,所以现在要删除掉这个索引: * * @param [type] $client * @return Array ( [acknowledged] => 1 ) 删除后再删除 Elasticsearch\Common\Exceptions\Missing404Exception */ public function demo5($client) { $deleteParams = [ 'index' => 'my_index' ]; $response = $client->indices()->delete($deleteParams); return $response; } /** * 创建一个索引edit * 由于数据已被清空,我们可以重新开始了,现在要添加一个索引,同时要进行自定义 settings: * * Elasticsearch会创建一个索引,并配置你指定的参数值,然后返回一个消息确认: * @param [type] $client * @return Array ( [acknowledged] => 1 [shards_acknowledged] => 1 [index] => my_index ) */ public function demo6($client) { $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 0 ] ] ]; $response = $client->indices()->create($params); return $response; }}