Repository navigation
Expand file tree
/
Copy pathElasticSearch.php
More file actions
122 lines (104 loc) · 3.03 KB
/
Copy pathElasticSearch.php
File metadata and controls
122 lines (104 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Created by PhpStorm.
* User: jett
* Date: 2019/01/04
* Time: 下午 03:54
*/
/**
* Class ElasticSearch
* ElasticSearch-sql封装类
* 依赖:GuzzleHttp,APP_PATH=应用根目录
*/
class ElasticSearch {
private static $server_url;
private static $sql;
private static $table;
private static $where;
private static $order;
private static $group;
private static $limit;
private static $http_client;
private static $config = [
'logs_path' => '/logs'
];
/**
* ElasticSearch constructor.
* @param array $config
* [
* 'server_url' = 'http://192.168.0.59:9200/'
* ]
* @throws Exception
*/
public function __construct(array $config = []) {
self::$http_client = new \GuzzleHttp\Client(['timeout' => 10.0]);
if(!empty($config)){
self::setServerUrl($config['server_url']);
}
}
/**
* 设置ElasticSearch地址
* @param string $server_url
* @throws Exception
*/
public static function setServerUrl(string $server_url){
if(empty(self::$http_client)){
self::$http_client = new \GuzzleHttp\Client(['timeout' => 10.0]);
}
$response_code = self::$http_client->get($server_url)->getStatusCode();
$response_code == 200 ?: self::newException('兄dei, 你这季白地址都ping不通的');
self::$server_url = $server_url.'/_sql?sql=';
}
/**
* 设置sql值
* @param string $sql
* @return bool
*/
public static function setSqlValue(string $sql) :bool {
self::$sql = $sql;
return true;
}
/**
* 查询
* @return array
* @throws Exception
*/
public static function select() {
if(!empty(self::$sql)){
return self::getData(self::$sql);
}
}
/**
* 请求数据
* @param string $sql
* @return array
* @throws Exception
*/
private static function getData(string $sql) :array {
$http_request = self::$http_client->get(self::$server_url.$sql);
($http_request->getStatusCode() == 200) ?: self::newException('兄dei, 你这季白地址都ping不通的');
$data = json_decode($http_request->getBody(), true);
$log = "sql:{$sql},\nhost:".self::$server_url.",\nreturn:";
!empty($data) ?: self::newExceptionPrintLog($log.$data,'兄dei,报错了啊,日志写了自己看去');
return $data;
}
/**
* @param string $msg
* @param int $code
* @throws Exception
*/
static private function newException(string $msg = "", int $code = -1) {
throw new Exception($msg, $code);
}
/**
* @param string $log
* @param string $msg
* @param int $code
* @throws Exception
*/
static private function newExceptionPrintLog(string $log, string $msg = "", int $code = -1){
$date = date('Y-m-d');
error_log($log."\n",3,APP_PATH.self::$config['logs_path']."/{$date}.log");
throw new Exception($msg, $code);
}
}