1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bardex\Elastic; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Query |
8
|
|
|
* @package Bardex\Elastic |
9
|
|
|
* @author Alexey Sumin <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
abstract class Query extends PrototypeQuery |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Получить собранный elasticsearch-запрос |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
abstract public function getQuery(); |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Отправить запрос на конкретный endpoint elasticsearch-сервера |
22
|
|
|
* @param array $query |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
abstract protected function executeQuery(array $query); |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Выполнить запрос к ES и вернуть результаты поиска. |
30
|
|
|
* @return SearchResult - возвращает набор документов |
31
|
|
|
*/ |
32
|
|
|
public function fetchAll() |
33
|
|
|
{ |
34
|
|
|
$response = $this->fetchRaw(); |
35
|
|
|
$result = $this->createSearchResult($response); |
36
|
|
|
return $result; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Выполнить запрос к ES и вернуть необработанный результат (с мета-данными). |
42
|
|
|
* @return array возвращает необработанный ответ ES |
43
|
|
|
*/ |
44
|
|
|
public function fetchRaw() |
45
|
|
|
{ |
46
|
|
|
// build query |
47
|
|
|
$query = $this->getQuery(); |
48
|
|
|
|
49
|
|
|
// send query to elastic |
50
|
|
|
$start = microtime(1); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$result = $this->executeQuery($query); |
54
|
|
|
$time = round((microtime(1) - $start) * 1000); |
55
|
|
|
$this->triggerSuccess($query, $result, $time); |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
catch (\Exception $e) { |
59
|
|
|
$this->triggerError($query, $e); |
60
|
|
|
throw $e; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Создать из ответа ES-сервера экземляр SearchResult |
67
|
|
|
* @param array $response |
68
|
|
|
* @return SearchResult |
69
|
|
|
*/ |
70
|
|
|
protected function createSearchResult(array $response) |
71
|
|
|
{ |
72
|
|
|
$results = $this->extractDocuments($response); |
73
|
|
|
$total = $this->extractTotal($response); |
74
|
|
|
$searchResult = new SearchResult($results, $total); |
75
|
|
|
return $searchResult; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Выбрать документы из ответа ES-сервера и добавить script fields. |
80
|
|
|
* @param array $response - ответ ES сервера. |
81
|
|
|
* @return array - возвращает набор документов |
82
|
|
|
*/ |
83
|
|
|
protected function extractDocuments(array $response) |
84
|
|
|
{ |
85
|
|
|
$results = []; |
86
|
|
|
if (isset($response['hits']['hits'])) { |
87
|
|
|
foreach ($response['hits']['hits'] as $hit) { |
88
|
|
|
$row = $hit['_source']; |
89
|
|
|
if (isset($hit['fields'])) { // script fields |
90
|
|
|
foreach ($hit['fields'] as $field => $data) { |
91
|
|
|
if (count($data) == 1) { |
92
|
|
|
$row[$field] = array_shift($data); |
93
|
|
|
} else { |
94
|
|
|
$row[$field] = $data; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$results[] = $row; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return $results; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Выбрать из ответа ES-сервера количество найденных документов. |
107
|
|
|
* @param array $response - ответ ES сервера. |
108
|
|
|
* @return integer - возвращает количество найденных документов. |
109
|
|
|
*/ |
110
|
|
|
protected function extractTotal(array $response) |
111
|
|
|
{ |
112
|
|
|
$total = 0; |
113
|
|
|
if (isset($response['hits']['total'])) { |
114
|
|
|
$total = $response['hits']['total']; |
115
|
|
|
} |
116
|
|
|
return $total; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function triggerSuccess(array $query, array $response, $time) |
120
|
|
|
{ |
121
|
|
|
foreach ($this->listeners as $listener) { |
122
|
|
|
$listener->onSuccess($query, $response, $time); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function triggerError(array $query, \Exception $e) |
127
|
|
|
{ |
128
|
|
|
foreach ($this->listeners as $listener) { |
129
|
|
|
$listener->onError($query, $e); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |