1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TonicHealthCheck\Check\Elasticsearch\GetDocument; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client as ElasticsearchClient; |
6
|
|
|
use Elasticsearch\Common\Exceptions\ElasticsearchException; |
7
|
|
|
use Exception; |
8
|
|
|
use TonicHealthCheck\Check\Elasticsearch\AbstractElasticsearchCheck; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ElasticsearchGetDocumentCheck. |
12
|
|
|
*/ |
13
|
|
|
class ElasticsearchGetDocumentCheck extends AbstractElasticsearchCheck |
14
|
|
|
{ |
15
|
|
|
const CHECK = 'elasticsearch-get-document-check'; |
16
|
|
|
|
17
|
|
|
const INDEX_GET_SIZE = 5; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ElasticsearchClient |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $index; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $minSize; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $checkNode |
41
|
|
|
* @param ElasticsearchClient $client |
42
|
|
|
* @param string $index |
43
|
|
|
* @param string $type |
44
|
|
|
* @param int $minSize |
45
|
|
|
*/ |
46
|
4 |
|
public function __construct($checkNode, ElasticsearchClient $client, $index, $type, $minSize = self::INDEX_GET_SIZE) |
47
|
|
|
{ |
48
|
4 |
|
parent::__construct($checkNode); |
49
|
4 |
|
$this->setClient($client); |
50
|
4 |
|
$this->setIndex($index); |
51
|
4 |
|
$this->setType($type); |
52
|
4 |
|
$this->setMinSize($minSize); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check elasticsearch client can get index type. |
57
|
|
|
* |
58
|
|
|
* @param string $index |
59
|
|
|
* @param string $type |
60
|
|
|
* @param int $minSize |
61
|
|
|
* @return void |
62
|
|
|
* |
63
|
|
|
* @throws ElasticsearchGetDocumentCheckException |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
4 |
|
public function performCheck($index = null, $type = null, $minSize = null) |
67
|
|
|
{ |
68
|
4 |
|
if (null === $index) { |
69
|
4 |
|
$index = $this->getIndex(); |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
if (null === $type) { |
73
|
4 |
|
$type = $this->getType(); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
if (null === $minSize) { |
77
|
4 |
|
$minSize = $this->getMinSize(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$params = [ |
81
|
4 |
|
'size' => static::INDEX_GET_SIZE, |
82
|
4 |
|
'index' => $index, |
83
|
4 |
|
'type' => $type, |
84
|
|
|
|
85
|
|
|
]; |
86
|
|
|
try { |
87
|
4 |
|
$response = $this->getClient()->search($params); |
88
|
2 |
|
} catch (Exception $e) { |
89
|
2 |
|
if (!$e instanceof ElasticsearchException) { |
90
|
1 |
|
throw $e; |
91
|
|
|
} |
92
|
1 |
|
throw ElasticsearchGetDocumentCheckException::internalGetProblem($e); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$size = !empty($response['hits']['total']) ? $response['hits']['total'] : 0; |
96
|
|
|
|
97
|
2 |
|
if ($size < $minSize) { |
98
|
1 |
|
throw ElasticsearchGetDocumentCheckException::emptyIndex($index, $type, $size, $minSize); |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return ElasticsearchClient |
104
|
|
|
*/ |
105
|
4 |
|
public function getClient() |
106
|
|
|
{ |
107
|
4 |
|
return $this->client; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
4 |
|
public function getIndex() |
114
|
|
|
{ |
115
|
4 |
|
return $this->index; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
4 |
|
public function getType() |
122
|
|
|
{ |
123
|
4 |
|
return $this->type; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
4 |
|
public function getMinSize() |
130
|
|
|
{ |
131
|
4 |
|
return $this->minSize; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $index |
136
|
|
|
*/ |
137
|
4 |
|
protected function setIndex($index) |
138
|
|
|
{ |
139
|
4 |
|
$this->index = $index; |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $type |
144
|
|
|
*/ |
145
|
4 |
|
protected function setType($type) |
146
|
|
|
{ |
147
|
4 |
|
$this->type = $type; |
148
|
4 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $minSize |
152
|
|
|
*/ |
153
|
4 |
|
protected function setMinSize($minSize) |
154
|
|
|
{ |
155
|
4 |
|
$this->minSize = $minSize; |
156
|
4 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param ElasticsearchClient $client |
160
|
|
|
*/ |
161
|
4 |
|
protected function setClient(ElasticsearchClient $client) |
162
|
|
|
{ |
163
|
4 |
|
$this->client = $client; |
164
|
4 |
|
} |
165
|
|
|
} |
166
|
|
|
|