-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elasticsearch.php
141 lines (127 loc) · 5.26 KB
/
Elasticsearch.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
class Elasticsearch extends CApplicationComponent {
public $host;
public $port;
public $prefix;
public $max_result_window = 10000;
public $checkAvailability = false;
private $url;
public function init() {
parent::init();
if(empty($this->host) == true) {
throw new Exception('Адрес сервера не указан', 500);
}
if(empty($this->port) == true) {
throw new Exception('Номер порта не указан', 500);
}
if(empty($this->prefix) == true) {
throw new Exception('Префикс для индекса не указан', 500);
}
$this->url = 'http://' . $this->host . ':' . $this->port;
$this->checkAvailability();
}
private function checkAvailability() {
if($this->checkAvailability == false) {
return;
}
$fp = fsockopen($this->host, $this->port, $errno, $errstr);
if (!$fp) {
throw new Exception('Указанный в настройках сервер недоступен', 500);
}
else {
fclose($fp);
}
}
public function getUrl() {
return $this->url;
}
public function getIndices($simple = true) {
$this->checkAvailability();
$response = Yii::app()->curl->get($this->getUrl() . '/_cat/indices', []);
if($simple == true) {
return $response;
}
return explode("\n", $response);
}
public function ifIndexExist($index) {
$this->checkAvailability();
$index = $this->prefix . $index;
$response = $this->getIndices(false);
$filter = array_filter($response,function($x) use($index) {
if(empty($x) == true) {
return false;
}
$tmp = explode(' ', $x);
$tmp = array_values(array_filter($tmp));
if($tmp[2] == $index) {
return true;
}
return false;
});
if(empty($filter) == true) {
return false;
}
return true;
}
public function deleteIndex($index) {
$this->checkAvailability();
if(empty($index) == true) {
throw new Exception('Название индекса не может быть пустым', 500);
}
$response = Yii::app()->curl->delete($this->getUrl() . '/' . $this->prefix . $index, []);
$this->checkError($response);
return $response;
}
public function createIndex($index, $data) {
$this->checkAvailability();
if(empty($index) == true) {
throw new Exception('Название индекса не может быть пустым', 500);
}
Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$response = Yii::app()->curl->put($this->getUrl() . '/' . $this->prefix . $index, json_encode($data));
$this->checkError($response);
return $response;
}
public function insert($index, $type, $data) {
$this->checkAvailability();
Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$response = Yii::app()->curl->post($this->getUrl() . '/' . $this->prefix . $index . '/' . $type, json_encode($data));
$this->checkError($response);
return $response;
}
public function search($index, $type, $criteria, $decoded = true) {
$this->checkAvailability();
Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$response = Yii::app()->curl->post($this->getUrl() . '/' . $this->prefix . $index . '/' . $type . '/_search', json_encode($criteria));
$this->checkError($response);
if($decoded == true) {
return json_decode($response, true);
}
return $response;
}
public function createMapping($index, $type, $data) {
$this->checkAvailability();
Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$response = Yii::app()->curl->put($this->getUrl() . '/' . $this->prefix . $index . '/' . $type . '/_mapping', json_encode($data));
$this->checkError($response);
return $response;
}
public function deleteByQuery($index, $type, $data) {
$this->checkAvailability();
Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=UTF-8']);
$response = Yii::app()->curl->post($this->getUrl() . '/' . $this->prefix . $index . '/' . $type . '/_delete_by_query', json_encode($data));
$this->checkError($response);
return $response;
}
public function checkError($response) {
$decoded = json_decode($response, true);
if(isset($decoded['error'])) {
if(is_array($decoded['error'])) {
throw new Exception(var_export($decoded['error'], true), $decoded['status']);
}
else {
throw new Exception($decoded['error'], $decoded['status']);
}
}
}
}