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