-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElasticSearchHelper.php
More file actions
58 lines (45 loc) · 1.72 KB
/
Copy pathElasticSearchHelper.php
File metadata and controls
58 lines (45 loc) · 1.72 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
<?php
namespace App\Helper;
use App\Http\Curl\HttpRequest;
class ElasticSearchHelper {
public function __construct(private readonly string $elasticSearchHost, private readonly string $elasticSearchBaseName)
{
}
public function hasIndices(HttpRequest $request): bool {
$hasIndices = true;
// Make an initial request to see if there is anything
$url = $this->elasticSearchHost . "/_cat/indices/{$this->elasticSearchBaseName}*?v&s=index&h=index";
$request->setOptions(
[
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => getenv('CURLOPT_TIMEOUT_ELASTICSEARCH_DELETE_CHECK') ?: 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
]
);
$rawResponse = $request->execute();
$err = $request->error();
$request->close();
if ($err) {
throw new \RuntimeException('curl error for ' . $this->elasticSearchBaseName . ': ' . $err);
}
// Example response:
//
// index\n
// site1.localhost_content_blabla\n
// site1.localhost_general_bla\n
$wikiIndices = array_filter(explode("\n", (string) $rawResponse));
// no indices to delete only index header
if (count($wikiIndices) <= 1) {
$hasIndices = false;
}
$indexHeader = array_shift($wikiIndices);
// make sure response is formatted correctly
if ($indexHeader !== 'index') {
throw new \RuntimeException("Response looks weird when querying {$url}");
}
return $hasIndices;
}
}