1. <?php
    2. namespace backend\controllers;
    3. use yii\web\Controller;
    4. /**
    5. * Site controller
    6. */
    7. class EsController extends Controller
    8. {
    9. public function actionIndex()
    10. {
    11. $client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://10.0.18.170:9200'])->build();
    12. // 新增
    13. // $demo1 = $this->demo1($client);
    14. // 查询
    15. // $demo2 = $this->demo2($client);
    16. // 筛选
    17. // $demo3 = $this->demo3($client);
    18. // 删除
    19. // $demo4 = $this->demo4($client);
    20. // 删除索引
    21. // $demo5 = $this->demo5($client);
    22. // 创建索引
    23. $demo6 = $this->demo6($client);
    24. header("content-type: text/html;charset=utf-8");
    25. echo "<pre>";
    26. print_r($demo6);
    27. exit;
    28. }
    29. /**
    30. * 索引一个文档
    31. *
    32. * 在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置。REST 路径(endpoint)、文档和可选参数都是用关联数组来配置。
    33. * 为了索引一个文档,我们要指定4部分信息:index,type,id 和一个 body。
    34. * 构建一个键值对的关联数组就可以完成上面的内容。body 的键值对格式与文档的数据保持一致性。
    35. * (译者注:如 ["testField" ⇒ "abc"] 在文档中则为 {"testField" : "abc"}):
    36. *
    37. * https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html
    38. * @return Array
    39. (
    40. [_index] => my_index
    41. [_type] => my_type
    42. [_id] => my_id
    43. [_version] => 1
    44. [result] => created
    45. [_shards] => Array
    46. (
    47. [total] => 2
    48. [successful] => 1
    49. [failed] => 0
    50. )
    51. [_seq_no] => 0
    52. [_primary_term] => 1
    53. )
    54. */
    55. public function demo1($client)
    56. {
    57. $params = [
    58. 'index' => 'my_index',
    59. 'type' => 'my_type',
    60. 'id' => 'my_id',
    61. 'body' => [
    62. 'testField' => 'abc'
    63. ]
    64. ];
    65. $response = $client->index($params);
    66. return $response;
    67. }
    68. /**
    69. * 现在获取刚才索引(demo1)的文档:
    70. *
    71. * @param [type] $client
    72. *
    73. * 响应数据包含一些元数据(如 index,type 等)和 _source 属性, 这是你发送给 Elasticsearch 的原始文档数据。
    74. * @return Array
    75. (
    76. [_index] => my_index
    77. [_type] => my_type
    78. [_id] => my_id
    79. [_version] => 3
    80. [_seq_no] => 2
    81. [_primary_term] => 1
    82. [found] => 1
    83. [_source] => Array
    84. (
    85. [testField] => abc
    86. )
    87. )
    88. */
    89. public function demo2($client)
    90. {
    91. $params = [
    92. 'index' => 'my_index',
    93. 'type' => 'my_type',
    94. 'id' => 'my_id',
    95. ];
    96. $response = $client->get($params);
    97. return $response;
    98. }
    99. /**
    100. * 搜索一个文档edit
    101. * 搜索是 elasticsearch 的一大特色,所以我们试一下执行一个搜索。我们准备用 Match 查询来作为示范:
    102. *
    103. * 这个响应数据与前面例子的响应数据有所不同。这里有一些元数据(如 took, timed_out 等)和一个 hits 的数组,
    104. * 这代表了你的搜索结果。而 hits 内部也有一个 hits 数组,内部的 hits 包含特定的搜索结果:
    105. * @param [type] $client
    106. * @return Array
    107. (
    108. [took] => 1
    109. [timed_out] =>
    110. [_shards] => Array
    111. (
    112. [total] => 1
    113. [successful] => 1
    114. [skipped] => 0
    115. [failed] => 0
    116. )
    117. [hits] => Array
    118. (
    119. [total] => Array
    120. (
    121. [value] => 1
    122. [relation] => eq
    123. )
    124. [max_score] => 0.18232156
    125. [hits] => Array
    126. (
    127. [0] => Array
    128. (
    129. [_index] => my_index
    130. [_type] => my_type
    131. [_id] => my_id
    132. [_score] => 0.18232156
    133. [_source] => Array
    134. (
    135. [testField] => abc
    136. )
    137. )
    138. )
    139. )
    140. )
    141. */
    142. public function demo3($client)
    143. {
    144. $params = [
    145. 'index' => 'my_index',
    146. 'type' => 'my_type',
    147. 'body' => [
    148. 'query' => [
    149. 'match' => [
    150. 'testField' => 'abc'
    151. ]
    152. ]
    153. ],
    154. ];
    155. $response = $client->search($params);
    156. return $response;
    157. }
    158. /**
    159. * 删除一个文档edit
    160. * 好了,现在我们看一下如何把之前添加的文档删除掉:
    161. *
    162. * @param [type] $client
    163. *
    164. * 你会注意到删除文档的语法与获取文档的语法是一样的。唯一不同的是 delete 方法替代了 get 方法。下面响应数据代表文档已被删除:
    165. * @return Array
    166. (
    167. [_index] => my_index
    168. [_type] => my_type
    169. [_id] => my_id
    170. [_version] => 4
    171. [result] => deleted
    172. [_shards] => Array
    173. (
    174. [total] => 2
    175. [successful] => 1
    176. [failed] => 0
    177. )
    178. [_seq_no] => 3
    179. [_primary_term] => 1
    180. )
    181. * 删除后再次删除
    182. *
    183. * Elasticsearch\Common\Exceptions\Missing404Exception:
    184. * {"_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}
    185. */
    186. public function demo4($client)
    187. {
    188. $params = [
    189. 'index' => 'my_index',
    190. 'type' => 'my_type',
    191. 'id' => 'my_id',
    192. ];
    193. $response = $client->delete($params);
    194. return $response;
    195. }
    196. /**
    197. * 删除一个索引edit
    198. * 由于 elasticsearch 的动态特性,我们创建的第一个文档会自动创建一个索引,
    199. * 同时也会把 settings 里面的参数设定为默认参数。由于我们在后面要指定特定的 settings,所以现在要删除掉这个索引:
    200. *
    201. * @param [type] $client
    202. * @return Array
    203. (
    204. [acknowledged] => 1
    205. )
    206. 删除后再删除
    207. Elasticsearch\Common\Exceptions\Missing404Exception
    208. */
    209. public function demo5($client)
    210. {
    211. $deleteParams = [
    212. 'index' => 'my_index'
    213. ];
    214. $response = $client->indices()->delete($deleteParams);
    215. return $response;
    216. }
    217. /**
    218. * 创建一个索引edit
    219. * 由于数据已被清空,我们可以重新开始了,现在要添加一个索引,同时要进行自定义 settings:
    220. *
    221. * Elasticsearch会创建一个索引,并配置你指定的参数值,然后返回一个消息确认:
    222. * @param [type] $client
    223. * @return Array
    224. (
    225. [acknowledged] => 1
    226. [shards_acknowledged] => 1
    227. [index] => my_index
    228. )
    229. */
    230. public function demo6($client)
    231. {
    232. $params = [
    233. 'index' => 'my_index',
    234. 'body' => [
    235. 'settings' => [
    236. 'number_of_shards' => 2,
    237. 'number_of_replicas' => 0
    238. ]
    239. ]
    240. ];
    241. $response = $client->indices()->create($params);
    242. return $response;
    243. }
    244. }