-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathElasticsearchResponse.php
More file actions
126 lines (107 loc) · 3.43 KB
/
ElasticsearchResponse.php
File metadata and controls
126 lines (107 loc) · 3.43 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
123
124
125
126
<?php
namespace G4\DataMapper\Engine\Elasticsearch;
use G4\DataMapper\Domain\TotalCount;
class ElasticsearchResponse
{
const KEY_HITS = 'hits';
const KEY_ERROR = 'error';
const KEY_TYPE = 'type';
const KEY_ROOT_CAUSE = 'root_cause';
const KEY_RESPONSES_M_SEARCH = 'responses';
const KEY_COUNT = 'count';
/**
* @var array
*/
private $decodedResponse;
/**
* @var string
*/
private $response;
/**
* ElasticsearchResponse constructor.
* @param $response
*/
public function __construct($response)
{
$this->response = $response;
$this->getDecodedResponse();
}
/**
* @return array
*/
public function getHits()
{
return array_key_exists(self::KEY_RESPONSES_M_SEARCH, $this->decodedResponse)
? $this->getHitFromMultiSearch() : $this->getHitsFromSearch();
}
/**
* @return int
*/
public function getTotal()
{
if (!is_array($this->decodedResponse)) {
return 0;
}
switch (true) {
case array_key_exists(self::KEY_RESPONSES_M_SEARCH, $this->decodedResponse):
return $this->getTotalFromMultiSearch();
case array_key_exists(self::KEY_HITS, $this->decodedResponse):
return (new TotalCount($this->decodedResponse[self::KEY_HITS]))->getValue();
case array_key_exists(self::KEY_COUNT, $this->decodedResponse):
return (new TotalCount($this->decodedResponse[self::KEY_COUNT]))->getValue();
case $this->hasError():
default:
return 0;
}
}
/**
* @return array
*/
private function getDecodedResponse(): ?array
{
if ($this->decodedResponse === null) {
$this->decodedResponse = is_string($this->response) ? json_decode($this->response, true) : null;
}
return $this->decodedResponse;
}
public function hasError()
{
return $this->decodedResponse === null || array_key_exists(self::KEY_ERROR, $this->decodedResponse);
}
public function getErrorMessage(): string
{
if ($this->decodedResponse === null) {
return json_encode(['Error decoding response', $this->response]);
}
return json_encode([
$this->decodedResponse[self::KEY_ERROR][self::KEY_TYPE],
$this->decodedResponse[self::KEY_ERROR][self::KEY_ROOT_CAUSE],
]);
}
private function getHitFromMultiSearch()
{
$multiSearchHits = [];
foreach ($this->decodedResponse[self::KEY_RESPONSES_M_SEARCH] as $singleHits) {
if (array_key_exists(self::KEY_HITS, $singleHits)) {
$multiSearchHits[] = $singleHits[self::KEY_HITS];
}
}
return $multiSearchHits;
}
private function getHitsFromSearch()
{
return array_key_exists(self::KEY_HITS, $this->decodedResponse)
? $this->decodedResponse[self::KEY_HITS]
: [];
}
private function getTotalFromMultiSearch(): int
{
$multiSearchTotal = 0;
foreach ($this->decodedResponse[self::KEY_RESPONSES_M_SEARCH] as $singleHits) {
if (!$this->hasError() && array_key_exists(self::KEY_HITS, $singleHits)) {
$multiSearchTotal += (new TotalCount($singleHits[self::KEY_HITS]))->getValue();
}
}
return $multiSearchTotal;
}
}