1 | <?php |
||
8 | class QueryBuilder |
||
9 | { |
||
10 | /** |
||
11 | * Query. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | protected $query = []; |
||
16 | |||
17 | /** |
||
18 | * Constructor. |
||
19 | * |
||
20 | * @param array $body |
||
21 | */ |
||
22 | 8 | public function __construct(array $body = []) |
|
26 | |||
27 | /** |
||
28 | * @param int $from |
||
29 | * @return $this |
||
30 | */ |
||
31 | public function from($from) |
||
32 | { |
||
33 | $this->query['from'] = $from; |
||
34 | return $this; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param int $size |
||
39 | * @return $this |
||
40 | */ |
||
41 | 2 | public function size($size) |
|
46 | |||
47 | /** |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function hasSort() |
||
54 | |||
55 | /** |
||
56 | * @param array|string $sort |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function sort($sort) |
||
64 | |||
65 | /** |
||
66 | * @param Filter|array $filter |
||
67 | * @param string $mode |
||
68 | * @return $this |
||
69 | */ |
||
70 | 6 | public function filter($filter = [], $mode = Filter::MODE_INCLUDE) |
|
93 | |||
94 | /** |
||
95 | * Set _source to search query. |
||
96 | * |
||
97 | * @param mixed $fields |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function fields($fields = false) |
||
101 | { |
||
102 | if ($fields === false) { |
||
103 | $this->query['body']['_source'] = false; |
||
104 | } elseif ((array)$fields == ['*']) { |
||
105 | unset($this->query['body']['_source']); |
||
106 | } else { |
||
107 | $this->query['body']['_source'] = $fields; |
||
108 | } |
||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Return only _id. |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function getOnlyIds() |
||
121 | |||
122 | /** |
||
123 | * Return query. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 8 | protected function getQuery() |
|
131 | |||
132 | /** |
||
133 | * Build query. |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 8 | public function build() |
|
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function __toString() |
||
149 | |||
150 | /** |
||
151 | * @param int $options |
||
152 | * @return string |
||
153 | */ |
||
154 | public function toJson($options = 0) |
||
158 | |||
159 | 6 | protected function merge(array $query, $mode = 'must') |
|
169 | |||
170 | 2 | public function aggregation(Aggregation $aggregation) |
|
175 | |||
176 | public function where($field, $value) |
||
182 | |||
183 | public function notWhere($field, $value) |
||
189 | |||
190 | public function orWhere($field, $value) |
||
196 | |||
197 | public function between($field, $start, $end) |
||
203 | |||
204 | public function betweenOrEquals($field, $start, $end) |
||
205 | { |
||
206 | $query = ['range' => [$field => ['gte' => $start, 'lte' => $end]]]; |
||
207 | $this->merge($query); |
||
208 | return $this; |
||
209 | } |
||
210 | |||
211 | public function range($field, $start, $end) |
||
215 | |||
216 | public function greaterThan($field, $start) |
||
217 | { |
||
218 | $query = ['range' => [$field => ['gt' => $start]]]; |
||
219 | $this->merge($query); |
||
220 | return $this; |
||
221 | } |
||
222 | |||
223 | public function gt($field, $start) |
||
227 | |||
228 | public function greaterThanOrEquals($field, $start) |
||
234 | |||
235 | public function gte($field, $start) |
||
239 | |||
240 | public function lessThan($field, $end) |
||
246 | |||
247 | public function lt($field, $start) |
||
251 | |||
252 | public function lessThanOrEquals($field, $end) |
||
258 | |||
259 | public function lte($field, $start) |
||
263 | |||
264 | public function match($field, $value) |
||
265 | { |
||
266 | $query = ['query' => ['match' => [$field => $value]]]; |
||
267 | $this->merge($query); |
||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | public function notMatch($field, $value) |
||
277 | |||
278 | public function orMatch($field, $value) |
||
279 | { |
||
280 | $query = ['query' => ['match' => [$field => $value]]]; |
||
281 | $this->merge($query, 'should'); |
||
282 | return $this; |
||
284 | } |
||
285 |