1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
7
|
|
|
* @package yii2-hiart |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\hiart; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\Component; |
16
|
|
|
use yii\db\QueryInterface; |
17
|
|
|
use yii\db\QueryTrait; |
18
|
|
|
|
19
|
|
|
class Query extends Component implements QueryInterface |
20
|
|
|
{ |
21
|
|
|
use QueryTrait; |
22
|
|
|
|
23
|
|
|
public $index; |
24
|
|
|
|
25
|
|
|
public $type; |
26
|
|
|
|
27
|
|
|
public $select; |
28
|
|
|
public $join; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function init() |
34
|
|
|
{ |
35
|
|
|
parent::init(); |
36
|
|
|
// setting the default limit according to api defaults |
37
|
|
|
if ($this->limit === null) { |
38
|
|
|
$this->limit = 'ALL'; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function createCommand($db = null) |
43
|
|
|
{ |
44
|
|
|
if ($db === null) { |
45
|
|
|
$db = Yii::$app->get('hiresource'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$commandConfig = $db->getQueryBuilder()->build($this); |
49
|
|
|
|
50
|
|
|
return $db->createCommand($commandConfig); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function join($type) |
54
|
|
|
{ |
55
|
|
|
$this->join[] = $type; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function all($db = null) |
61
|
|
|
{ |
62
|
|
|
$result = $this->createCommand($db)->search(); |
63
|
|
|
if (empty($result['hits']['hits'])) { |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
$rows = $result['hits']['hits']; |
67
|
|
|
if ($this->indexBy === null) { |
68
|
|
|
return $rows; |
69
|
|
|
} |
70
|
|
|
$models = []; |
71
|
|
|
foreach ($rows as $key => $row) { |
72
|
|
View Code Duplication |
if ($this->indexBy !== null) { |
|
|
|
|
73
|
|
|
if (is_string($this->indexBy)) { |
74
|
|
|
$key = isset($row['fields'][$this->indexBy]) ? reset($row['fields'][$this->indexBy]) : $row['_source'][$this->indexBy]; |
75
|
|
|
} else { |
76
|
|
|
$key = call_user_func($this->indexBy, $row); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$models[$key] = $row; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $models; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function one($db = null) |
86
|
|
|
{ |
87
|
|
|
$result = $this->createCommand($db)->search(['limit' => 1]); |
88
|
|
|
if (empty($result)) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
$record = reset($result); |
92
|
|
|
|
93
|
|
|
return $record; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function search($db = null, $options = []) |
97
|
|
|
{ |
98
|
|
|
$result = $this->createCommand($db)->search($options); |
99
|
|
|
if (!empty($result) && $this->indexBy !== null) { |
100
|
|
|
$rows = []; |
101
|
|
|
foreach ($result as $key => $row) { |
102
|
|
View Code Duplication |
if (is_string($this->indexBy)) { |
|
|
|
|
103
|
|
|
$key = isset($row['fields'][$this->indexBy]) ? $row['fields'][$this->indexBy] : $row['_source'][$this->indexBy]; |
104
|
|
|
} else { |
105
|
|
|
$key = call_user_func($this->indexBy, $row); |
106
|
|
|
} |
107
|
|
|
$rows[$key] = $row; |
108
|
|
|
} |
109
|
|
|
$result = $rows; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function delete($db = null, $options = []) |
116
|
|
|
{ |
117
|
|
|
return $this->createCommand($db)->deleteByQuery($options); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function scalar($field, $db = null) |
121
|
|
|
{ |
122
|
|
|
$record = self::one($db); |
123
|
|
|
if ($record !== false) { |
124
|
|
|
if ($field === '_id') { |
125
|
|
|
return $record['_id']; |
126
|
|
|
} elseif (isset($record['_source'][$field])) { |
127
|
|
|
return $record['_source'][$field]; |
128
|
|
|
} elseif (isset($record['fields'][$field])) { |
129
|
|
|
return count($record['fields'][$field]) === 1 ? reset($record['fields'][$field]) : $record['fields'][$field]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function column($field, $db = null) |
137
|
|
|
{ |
138
|
|
|
$command = $this->createCommand($db); |
139
|
|
|
$command->queryParts['_source'] = [$field]; |
140
|
|
|
$result = $command->search(); |
141
|
|
|
if (empty($result['hits']['hits'])) { |
142
|
|
|
return []; |
143
|
|
|
} |
144
|
|
|
$column = []; |
145
|
|
|
foreach ($result['hits']['hits'] as $row) { |
146
|
|
|
if (isset($row['fields'][$field])) { |
147
|
|
|
$column[] = $row['fields'][$field]; |
148
|
|
|
} elseif (isset($row['_source'][$field])) { |
149
|
|
|
$column[] = $row['_source'][$field]; |
150
|
|
|
} else { |
151
|
|
|
$column[] = null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $column; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function count($q = '*', $db = null) |
159
|
|
|
{ |
160
|
|
|
$options = []; |
161
|
|
|
$options['count'] = 1; |
162
|
|
|
|
163
|
|
|
return $this->createCommand($db)->search($options); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function exists($db = null) |
167
|
|
|
{ |
168
|
|
|
return self::one($db) !== false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function stats($groups) |
172
|
|
|
{ |
173
|
|
|
$this->stats = $groups; |
|
|
|
|
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function highlight($highlight) |
179
|
|
|
{ |
180
|
|
|
$this->highlight = $highlight; |
|
|
|
|
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function addAggregation($name, $type, $options) |
186
|
|
|
{ |
187
|
|
|
$this->aggregations[$name] = [$type => $options]; |
|
|
|
|
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function addAgg($name, $type, $options) |
193
|
|
|
{ |
194
|
|
|
return $this->addAggregation($name, $type, $options); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function addSuggester($name, $definition) |
198
|
|
|
{ |
199
|
|
|
$this->suggest[$name] = $definition; |
|
|
|
|
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function query($query) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$this->query = $query; |
|
|
|
|
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function filter($filter) |
212
|
|
|
{ |
213
|
|
|
$this->filter = $filter; |
|
|
|
|
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function from($index, $type = null) |
219
|
|
|
{ |
220
|
|
|
$this->index = $index; |
221
|
|
|
$this->type = $type; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
View Code Duplication |
public function fields($fields) |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
if (is_array($fields) || $fields === null) { |
229
|
|
|
$this->fields = $fields; |
|
|
|
|
230
|
|
|
} else { |
231
|
|
|
$this->fields = func_get_args(); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
View Code Duplication |
public function source($source) |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
if (is_array($source) || $source === null) { |
240
|
|
|
$this->source = $source; |
|
|
|
|
241
|
|
|
} else { |
242
|
|
|
$this->source = func_get_args(); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function timeout($timeout) |
249
|
|
|
{ |
250
|
|
|
$this->timeout = $timeout; |
|
|
|
|
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.