forked from pdphilip/laravel-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDslBuilder.php
More file actions
326 lines (260 loc) · 7.53 KB
/
Copy pathDslBuilder.php
File metadata and controls
326 lines (260 loc) · 7.53 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
declare(strict_types=1);
namespace PDPhilip\Elasticsearch\Query\DSL;
use RuntimeException;
class DslBuilder
{
protected array $dsl = [];
protected array $path = [];
public static function make(): self
{
return new static;
}
// ----------------------------------------------------------------------
// Getters
// ----------------------------------------------------------------------
public function getDsl(): array
{
return $this->dsl;
}
public function getBodyValue(array $keys)
{
return $this->getValueAtPath($this->dsl, ['body', ...$keys]);
}
// ----------------------------------------------------------------------
// Setters
// ----------------------------------------------------------------------
public function setIndex($index)
{
return $this->set(['index'], $index);
}
public function setBody(array $keys, $value)
{
return $this->set(['body', ...$keys], $value);
}
public function unsetBody(array $keys)
{
$this->unsetKeyAtPath($this->dsl, ['body', ...$keys]);
}
public function setFrom(int $from): self
{
return $this->set(['from'], $from);
}
/**
* Set the size part (for pagination)
*/
public function setSize(int $size): self
{
return $this->set(['size'], $size);
}
/**
* Set the _source part (for field selection)
*/
public function setSource($fields): self
{
return $this->set(['_source'], $fields);
}
/**
* Set the highlight part
*/
public function setHighlight(array $highlight): self
{
return $this->set(['highlight'], $highlight);
}
/**
* Add an aggregation
*/
public function setAggregation(string $name, array $aggregation): self
{
return $this->set(['aggs', $name], $aggregation);
}
/**
* Set the script part for updates
*/
public function setScript(string $source, array $params = [], array $options = []): self
{
$script = array_merge(['source' => $source, 'params' => $params], $options);
return $this->set(['script'], $script);
}
/**
* Add fields to the fields array (_source restriction)
*/
public function setFields(array $fields): self
{
return $this->set(['fields'], $fields);
}
/**
* Set a refresh parameter
* Accepts: true, false, or 'wait_for'
*/
public function setRefresh(bool|string $refresh = true): self
{
return $this->set(['refresh'], $refresh);
}
/**
* Set conflicts handling
*/
public function setConflicts(string $conflicts): self
{
return $this->set(['conflicts'], $conflicts);
}
/**
* Set routing parameter
*/
public function setRouting(string $routing): self
{
return $this->set(['routing'], $routing);
}
/**
* Add a post_filter
*/
public function setPostFilter(array $filter): self
{
return $this->set(['post_filter'], $filter);
}
public function setOpType(string $type): self
{
return $this->set(['op_type'], $type);
}
public function setOption(array $keys, $value): self
{
return $this->set($keys, $value);
}
public function loadDsl($dsl): self
{
$this->dsl = $dsl;
return $this;
}
public function appendOption(array $keys, $value): self
{
$current = $this->getValueAtPath($this->dsl, $keys);
if ($current) {
if (empty($current[0])) {
$current = [$current];
}
} else {
$current = [];
}
$current[] = $value;
return $this->set($keys, $current);
}
public function unsetOption(array $keys): self
{
$this->unsetKeyAtPath($this->dsl, $keys);
return $this;
}
// ----------------------------------------------------------------------
// Bulk
// ----------------------------------------------------------------------
public function appendBody(array $payload): self
{
return $this->path('body')->append($payload);
}
// ----------------------------------------------------------------------
// Internal
// ----------------------------------------------------------------------
/**
* Set a value at the specified path
*/
protected function set(array $path, $value): self
{
$this->setValueAtPath($this->dsl, $path, $value);
return $this;
}
/**
* Start building a path in the DSL structure
*/
protected function path(string ...$segments): self
{
$this->path = $segments;
return $this;
}
/**
* Add a segment to the current path
*/
protected function addPath(string ...$segments): self
{
$this->path = array_merge($this->path, $segments);
return $this;
}
/**
* Set a value at the current path
*/
protected function value($value): self
{
if (empty($this->path)) {
throw new RuntimeException('No path specified for DSL value');
}
$this->setValueAtPath($this->dsl, $this->path, $value);
$this->path = [];
return $this;
}
/**
* Append a value to an array at the current path
*/
protected function append($value): self
{
if (empty($this->path)) {
throw new RuntimeException('No path specified for DSL append');
}
$array = $this->getValueAtPath($this->dsl, $this->path) ?? [];
if (! is_array($array)) {
throw new RuntimeException('Cannot append to a non-array value');
}
$array[] = $value;
$this->setValueAtPath($this->dsl, $this->path, $array);
return $this;
}
protected function merge(array $value): self
{
if (empty($this->path)) {
throw new RuntimeException('No path specified for DSL merge');
}
$current = $this->getValueAtPath($this->dsl, $this->path) ?? [];
if (! is_array($current)) {
throw new RuntimeException('Cannot merge with a non-array value');
}
$merged = array_merge($current, $value);
$this->setValueAtPath($this->dsl, $this->path, $merged);
return $this;
}
// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
protected function unsetKeyAtPath(array &$array, array $path): void
{
$current = &$array;
foreach ($path as $index => $key) {
if (! isset($current[$key])) {
return;
}
if ($index === array_key_last($path)) {
unset($current[$key]);
return;
}
$current = &$current[$key];
}
}
protected function setValueAtPath(array &$array, array $path, $value): void
{
$current = &$array;
foreach ($path as $key) {
if (! isset($current[$key]) || ! is_array($current[$key])) {
$current[$key] = [];
}
$current = &$current[$key];
}
$current = $value;
}
protected function getValueAtPath(array $array, array $path)
{
$current = $array;
foreach ($path as $key) {
if (! isset($current[$key])) {
return null;
}
$current = $current[$key];
}
return $current;
}
}