Elastic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\common\service;
  3. use AlibabaCloud\SDK\Elasticsearch\V20170613\Elasticsearch;
  4. use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListILMPoliciesRequest;
  5. use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListInstanceIndicesRequest;
  6. use \Exception;
  7. use AlibabaCloud\Tea\Exception\TeaError;
  8. use AlibabaCloud\Tea\Utils\Utils;
  9. use Darabonba\OpenApi\Models\Config;
  10. use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListDataStreamsRequest;
  11. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  12. use Elastic\Elasticsearch\ClientBuilder;
  13. //require_once '../vendor/autoload.php';
  14. require_once '../vendor/elastic/transport/src/NodePool/NodePoolInterface.php';
  15. require_once '../vendor/elastic/transport/src/NodePool/Selector/RoundRobin.php';
  16. require_once '../vendor/elastic/transport/src/NodePool/Resurrect/NoResurrect.php';
  17. require_once '../vendor/elasticsearch/elasticsearch/src/ClientBuilder.php';
  18. require_once '../vendor/elastic/transport/src/TransportBuilder.php';
  19. require_once '../vendor/elastic/transport/src/NodePool/SimpleNodePool.php';
  20. require_once '../vendor/elastic/transport/src/Exception/NoNodeAvailableException.php';
  21. require_once '../vendor/elastic/transport/src/NodePool/Node.php';
  22. require_once '../vendor/elastic/transport/src/Transport.php';
  23. require_once '../vendor/php-http/httplug/src/HttpAsyncClient.php';
  24. require_once '../vendor/php-http/discovery/src/Psr17FactoryDiscovery.php';
  25. require_once '../vendor/php-http/discovery/src/Exception/NoCandidateFoundException.php';
  26. require_once '../vendor/php-http/discovery/src/Strategy/CommonPsr17ClassesStrategy.php';
  27. /**
  28. * Elasticsearch
  29. * Class Elasticsearch
  30. */
  31. class Elastic
  32. {
  33. static $access_key = 'LTAI5tJ5p12drZegeWVG33xZ';// key
  34. static $access_secret = '82UWAiY5e5wH8tSkRvMtqVoGO0h8SB';//secret
  35. static $InstanceId = 'es-cn-wwo34hg5t0006xtsj';
  36. static $test_index = 'product_info';
  37. public static function es(){
  38. static $es;
  39. if(!$es){
  40. $es= ClientBuilder::create()->build();
  41. }
  42. return $es;
  43. }
  44. /**
  45. * 使用AK&SK初始化账号Client
  46. * @param string $accessKeyId
  47. * @param string $accessKeySecret
  48. * @return Elasticsearch Client
  49. */
  50. public static function createClient(){
  51. $config = new Config([
  52. // 必填,您的 AccessKey ID
  53. "accessKeyId" => static::$access_key,
  54. // 必填,您的 AccessKey Secret
  55. "accessKeySecret" => static::$access_secret,
  56. ]);
  57. // 访问的域名
  58. $config->endpoint = "elasticsearch.cn-shanghai.aliyuncs.com";
  59. //$config->endpoint = "es-cn-wwo34hg5t0006xtsj.elasticsearch.aliyuncs.com";
  60. return new Elasticsearch($config);
  61. }
  62. public static function body()
  63. {
  64. return [
  65. 'mappings'=>[
  66. 'properties'=>[
  67. 'id'=>['type'=>'long'],
  68. 'status'=>['type'=>'byte'],
  69. 'logo'=>['type'=>'text'],
  70. 'name'=>['type'=>'text'],
  71. ]
  72. ]
  73. ];
  74. }
  75. //ES集群数据流列表
  76. public static function ListDataStreams()
  77. {
  78. $client = self::createClient();
  79. $listDataStreamsRequest = new ListDataStreamsRequest([]);
  80. $runtime = new RuntimeOptions([]);
  81. $headers = [];
  82. // 复制代码运行请自行打印 API 的返回值
  83. try {
  84. // 复制代码运行请自行打印 API 的返回值
  85. $res = $client->listDataStreamsWithOptions(static::$InstanceId, $listDataStreamsRequest, $headers, $runtime);
  86. $json_res = json_encode($res,true);
  87. $res_arr = json_decode($json_res,true);
  88. }
  89. catch (Exception $error) {
  90. // if (!($error instanceof TeaError)) $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  91. if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
  92. }
  93. return ['code'=>200,'data'=>$res_arr];
  94. }
  95. //查询指定索引生命周期详情
  96. public static function DescribeILMPolicy()
  97. {
  98. $client = self::createClient();
  99. $runtime = new RuntimeOptions([]);
  100. $headers = [];
  101. try {
  102. // 复制代码运行请自行打印 API 的返回值
  103. $res= $client->describeILMPolicyWithOptions(static::$InstanceId, 'product_info', $headers, $runtime);
  104. $json_res = json_encode($res,true);
  105. $res_arr = json_decode($json_res,true);
  106. } catch (Exception $error) {
  107. if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
  108. }
  109. return ['code'=>200,'data'=>$res_arr];
  110. }
  111. //获取集群的索引列表
  112. public static function ListInstanceIndices()
  113. {
  114. $client = self::createClient();
  115. $listInstanceIndicesRequest = new ListInstanceIndicesRequest([]);
  116. $runtime = new RuntimeOptions([]);
  117. $headers = [];
  118. try {
  119. // 复制代码运行请自行打印 API 的返回值
  120. $res = $client->listInstanceIndicesWithOptions(static::$InstanceId, $listInstanceIndicesRequest, $headers, $runtime);
  121. $json_res = json_encode($res,true);
  122. $res_arr = json_decode($json_res,true);
  123. } catch (Exception $error) {
  124. if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
  125. }
  126. return ['code'=>200,'data'=>$res_arr];
  127. }
  128. public static function getList()
  129. {
  130. $list = static ::es()->search([
  131. 'index'=>static::$test_index,
  132. ]);
  133. var_dump($list);die();
  134. return $list;
  135. }
  136. }