123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\common\service;
- use AlibabaCloud\SDK\Elasticsearch\V20170613\Elasticsearch;
- use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListILMPoliciesRequest;
- use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListInstanceIndicesRequest;
- use \Exception;
- use AlibabaCloud\Tea\Exception\TeaError;
- use AlibabaCloud\Tea\Utils\Utils;
- use Darabonba\OpenApi\Models\Config;
- use AlibabaCloud\SDK\Elasticsearch\V20170613\Models\ListDataStreamsRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use Elastic\Elasticsearch\ClientBuilder;
- //require_once '../vendor/autoload.php';
- require_once '../vendor/elastic/transport/src/NodePool/NodePoolInterface.php';
- require_once '../vendor/elastic/transport/src/NodePool/Selector/RoundRobin.php';
- require_once '../vendor/elastic/transport/src/NodePool/Resurrect/NoResurrect.php';
- require_once '../vendor/elasticsearch/elasticsearch/src/ClientBuilder.php';
- require_once '../vendor/elastic/transport/src/TransportBuilder.php';
- require_once '../vendor/elastic/transport/src/NodePool/SimpleNodePool.php';
- require_once '../vendor/elastic/transport/src/Exception/NoNodeAvailableException.php';
- require_once '../vendor/elastic/transport/src/NodePool/Node.php';
- require_once '../vendor/elastic/transport/src/Transport.php';
- require_once '../vendor/php-http/httplug/src/HttpAsyncClient.php';
- require_once '../vendor/php-http/discovery/src/Psr17FactoryDiscovery.php';
- require_once '../vendor/php-http/discovery/src/Exception/NoCandidateFoundException.php';
- require_once '../vendor/php-http/discovery/src/Strategy/CommonPsr17ClassesStrategy.php';
- /**
- * Elasticsearch
- * Class Elasticsearch
- */
- class Elastic
- {
- static $access_key = 'LTAI5tJ5p12drZegeWVG33xZ';// key
- static $access_secret = '82UWAiY5e5wH8tSkRvMtqVoGO0h8SB';//secret
- static $InstanceId = 'es-cn-wwo34hg5t0006xtsj';
- static $test_index = 'product_info';
- public static function es(){
- static $es;
- if(!$es){
- $es= ClientBuilder::create()->build();
- }
- return $es;
- }
- /**
- * 使用AK&SK初始化账号Client
- * @param string $accessKeyId
- * @param string $accessKeySecret
- * @return Elasticsearch Client
- */
- public static function createClient(){
- $config = new Config([
- // 必填,您的 AccessKey ID
- "accessKeyId" => static::$access_key,
- // 必填,您的 AccessKey Secret
- "accessKeySecret" => static::$access_secret,
- ]);
- // 访问的域名
- $config->endpoint = "elasticsearch.cn-shanghai.aliyuncs.com";
- //$config->endpoint = "es-cn-wwo34hg5t0006xtsj.elasticsearch.aliyuncs.com";
- return new Elasticsearch($config);
- }
- public static function body()
- {
- return [
- 'mappings'=>[
- 'properties'=>[
- 'id'=>['type'=>'long'],
- 'status'=>['type'=>'byte'],
- 'logo'=>['type'=>'text'],
- 'name'=>['type'=>'text'],
- ]
- ]
- ];
- }
- //ES集群数据流列表
- public static function ListDataStreams()
- {
- $client = self::createClient();
- $listDataStreamsRequest = new ListDataStreamsRequest([]);
- $runtime = new RuntimeOptions([]);
- $headers = [];
- // 复制代码运行请自行打印 API 的返回值
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->listDataStreamsWithOptions(static::$InstanceId, $listDataStreamsRequest, $headers, $runtime);
- $json_res = json_encode($res,true);
- $res_arr = json_decode($json_res,true);
- }
- catch (Exception $error) {
- // if (!($error instanceof TeaError)) $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
- }
- return ['code'=>200,'data'=>$res_arr];
- }
- //查询指定索引生命周期详情
- public static function DescribeILMPolicy()
- {
- $client = self::createClient();
- $runtime = new RuntimeOptions([]);
- $headers = [];
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res= $client->describeILMPolicyWithOptions(static::$InstanceId, 'product_info', $headers, $runtime);
- $json_res = json_encode($res,true);
- $res_arr = json_decode($json_res,true);
- } catch (Exception $error) {
- if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
- }
- return ['code'=>200,'data'=>$res_arr];
- }
- //获取集群的索引列表
- public static function ListInstanceIndices()
- {
- $client = self::createClient();
- $listInstanceIndicesRequest = new ListInstanceIndicesRequest([]);
- $runtime = new RuntimeOptions([]);
- $headers = [];
- try {
- // 复制代码运行请自行打印 API 的返回值
- $res = $client->listInstanceIndicesWithOptions(static::$InstanceId, $listInstanceIndicesRequest, $headers, $runtime);
- $json_res = json_encode($res,true);
- $res_arr = json_decode($json_res,true);
- } catch (Exception $error) {
- if (!($error instanceof TeaError)) return ['code'=>201,'data'=>null];
- }
- return ['code'=>200,'data'=>$res_arr];
- }
- public static function getList()
- {
- $list = static ::es()->search([
- 'index'=>static::$test_index,
- ]);
- var_dump($list);die();
- return $list;
- }
- }
|