企微短剧业务系统

EsNative.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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->delete($data);
  47. } catch (\Exception $e) {
  48. $resp = $e->getMessage();
  49. }
  50. return $resp;
  51. }
  52. // -------------------------------- 文档操作
  53. /**
  54. * 插入数据
  55. * @param $params
  56. * @return array|callable|string
  57. */
  58. public function addDoc($params) {
  59. try {
  60. $resp = $this->client->index($params);
  61. } catch (\Exception $e) {
  62. $resp = $e->getMessage();
  63. }
  64. return $resp;
  65. }
  66. /**
  67. * 更新数据
  68. * @param $params
  69. * @return array|callable|string
  70. */
  71. public function updateDoc($params) {
  72. try {
  73. $resp = $this->client->update($params);
  74. } catch (\Exception $e) {
  75. $resp = $e->getMessage();
  76. }
  77. return $resp;
  78. }
  79. /**
  80. * 删除数据
  81. * @param $params
  82. * @return array|callable|string
  83. */
  84. public function deleteDoc($params) {
  85. try {
  86. $resp = $this->client->delete($params);
  87. } catch (\Exception $e) {
  88. $resp = $e->getMessage();
  89. }
  90. return $resp;
  91. }
  92. /**
  93. * 查询数据
  94. * @param $params
  95. * @return array|callable|string
  96. */
  97. public function searchDoc($params) {
  98. try {
  99. $resp = $this->client->search($params);
  100. } catch (\Exception $e) {
  101. $resp = $e->getMessage();
  102. }
  103. return $resp;
  104. }
  105. /**
  106. * 批量操作
  107. * @param $data
  108. * @return array|callable|string
  109. */
  110. public function bulkDoc($data)
  111. {
  112. try {
  113. $resp = $this->client->bulk($data);
  114. } catch (\Exception $e) {
  115. $resp = $e->getMessage();
  116. }
  117. return $resp;
  118. }
  119. /**
  120. * 计算多少条数
  121. * @param $params
  122. * @return array|callable|string
  123. */
  124. public function countDoc($params) {
  125. try {
  126. $resp = $this->client->count($params);
  127. } catch (\Exception $e) {
  128. $resp = $e->getMessage();
  129. }
  130. return $resp;
  131. }
  132. // -------------------------------- 其他
  133. /**
  134. * 添加别名
  135. * @param $params
  136. * @return array|string
  137. */
  138. public function addAlias($params)
  139. {
  140. try {
  141. $resp = $this->client->indices()->updateAliases($params);
  142. } catch (\Exception $e) {
  143. $resp = $e->getMessage();
  144. }
  145. return $resp;
  146. }
  147. }