Nessuna descrizione

EsNative.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Components;
  3. use Elasticsearch\ClientBuilder;
  4. class EsNative {
  5. private $client;
  6. public function __construct(array $config) {
  7. $host = $config['host'];
  8. $port = $config['port'];
  9. $user = $config['user'];
  10. $pass = $config['pass'];
  11. $this->client = ClientBuilder::create()->setHosts([
  12. [
  13. 'host' => $host,
  14. 'port' => $port,
  15. 'scheme' => 'http',
  16. 'user' => $user,
  17. 'pass' => $pass
  18. ]
  19. ])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
  20. ->setRetries(10)->build();
  21. }
  22. // -------------------------------- 索引操作
  23. /**
  24. * 创建索引
  25. * @param $params
  26. * @return array|string
  27. */
  28. public function createIdx($params) {
  29. try {
  30. $resp = $this->client->indices()->create($params);
  31. } catch (\Exception $e) {
  32. $resp = $e->getMessage();
  33. }
  34. return $resp;
  35. }
  36. /**
  37. * 删除索引
  38. * @param $index
  39. * @return array|callable|string
  40. */
  41. public function deleteIdx($index) {
  42. $data = [
  43. 'index' => $index
  44. ];
  45. try {
  46. $resp = $this->client->indices()->delete($data);
  47. } catch (\Exception $e) {
  48. $resp = $e->getMessage();
  49. }
  50. return $resp;
  51. }
  52. /**
  53. * 是否存在索引
  54. * @param $index
  55. * @return array|callable|string
  56. */
  57. public function existsIdx($index) {
  58. $data = [
  59. 'index' => $index
  60. ];
  61. try {
  62. $resp = $this->client->indices()->exists($data);
  63. } catch (\Exception $e) {
  64. $resp = $e->getMessage();
  65. }
  66. return $resp;
  67. }
  68. // -------------------------------- 文档操作
  69. /**
  70. * 插入数据
  71. * @param $params
  72. * @return array|callable|string
  73. */
  74. public function addDoc($params) {
  75. try {
  76. $resp = $this->client->index($params);
  77. } catch (\Exception $e) {
  78. $resp = $e->getMessage();
  79. }
  80. return $resp;
  81. }
  82. /**
  83. * 更新数据
  84. * @param $params
  85. * @return array|callable|string
  86. */
  87. public function updateDoc($params) {
  88. try {
  89. $resp = $this->client->update($params);
  90. } catch (\Exception $e) {
  91. $resp = $e->getMessage();
  92. }
  93. return $resp;
  94. }
  95. /**
  96. * 删除数据
  97. * @param $params
  98. * @return array|callable|string
  99. */
  100. public function deleteDoc($params) {
  101. try {
  102. $resp = $this->client->delete($params);
  103. } catch (\Exception $e) {
  104. $resp = $e->getMessage();
  105. }
  106. return $resp;
  107. }
  108. /**
  109. * 查询数据
  110. * @param $params
  111. * @return array|callable|string
  112. */
  113. public function searchDoc($params) {
  114. try {
  115. $resp = $this->client->search($params);
  116. } catch (\Exception $e) {
  117. $resp = $e->getMessage();
  118. }
  119. return $resp;
  120. }
  121. /**
  122. * 批量操作
  123. * @param $data
  124. * @return array|callable|string
  125. */
  126. public function bulkDoc($data)
  127. {
  128. try {
  129. $resp = $this->client->bulk($data);
  130. } catch (\Exception $e) {
  131. $resp = $e->getMessage();
  132. }
  133. return $resp;
  134. }
  135. /**
  136. * 计算多少条数
  137. * @param $params
  138. * @return array|callable|string
  139. */
  140. public function countDoc($params) {
  141. try {
  142. $resp = $this->client->count($params);
  143. } catch (\Exception $e) {
  144. $resp = $e->getMessage();
  145. }
  146. return $resp;
  147. }
  148. // -------------------------------- 其他
  149. /**
  150. * 添加别名
  151. * @param $params
  152. * @return array|string
  153. */
  154. public function addAlias($params)
  155. {
  156. try {
  157. $resp = $this->client->indices()->updateAliases($params);
  158. } catch (\Exception $e) {
  159. $resp = $e->getMessage();
  160. }
  161. return $resp;
  162. }
  163. }