1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es\Mapping\Mapping; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Fluent; |
5
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentBinary; |
6
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentBoolean; |
7
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentByte; |
8
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentCompletion; |
9
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentDate; |
10
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentDateRange; |
11
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentDouble; |
12
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentDoubleRange; |
13
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentFloat; |
14
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentFloatRange; |
15
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentGeoPoint; |
16
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentGeoShape; |
17
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentHalfFloat; |
18
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentInteger; |
19
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentIntegerRange; |
20
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentIp; |
21
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentIpRange; |
22
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentKeyword; |
23
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentLong; |
24
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentLongRange; |
25
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentNested; |
26
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentObject; |
27
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentScaledFloat; |
28
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentShort; |
29
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentText; |
30
|
|
|
use Triadev\Es\Mapping\Mapping\Fluent\FluentTokenCount; |
31
|
|
|
|
32
|
|
|
class Compiler |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Compile: text |
36
|
|
|
* |
37
|
|
|
* @param FluentText $fluent |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
5 |
|
public function compileText(FluentText $fluent) : array |
41
|
|
|
{ |
42
|
5 |
|
return $this->formatAttributes([ |
43
|
5 |
|
'type' => 'text', |
44
|
5 |
|
'analyzer' => $fluent->analyzer, |
45
|
5 |
|
'boost' => $fluent->boost, |
46
|
5 |
|
'eager_global_ordinals' => $fluent->eager_global_ordinals, |
47
|
5 |
|
'fielddata' => $fluent->fielddata, |
48
|
5 |
|
'fielddata_frequency_filter' => $fluent->fielddata_frequency_filter, |
49
|
5 |
|
'fields' => $fluent->fields, |
50
|
5 |
|
'index' => $fluent->index, |
51
|
5 |
|
'index_options' => $fluent->index_options, |
52
|
5 |
|
'index_prefixes' => $fluent->index_prefixes, |
53
|
5 |
|
'index_phrases' => $fluent->index_phrases, |
54
|
5 |
|
'norms' => $fluent->norms, |
55
|
5 |
|
'position_increment_gap' => $fluent->position_increment_gap, |
56
|
5 |
|
'store' => $fluent->store, |
57
|
5 |
|
'search_analyzer' => $fluent->search_analyzer, |
58
|
5 |
|
'search_quote_analyzer' => $fluent->search_quote_analyzer, |
59
|
5 |
|
'similarity' => $fluent->similarity, |
60
|
5 |
|
'term_vector' => $fluent->term_vector |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Compile: keyword |
66
|
|
|
* |
67
|
|
|
* @param FluentKeyword $fluent |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
6 |
|
public function compileKeyword(FluentKeyword $fluent) : array |
71
|
|
|
{ |
72
|
6 |
|
return $this->formatAttributes([ |
73
|
6 |
|
'type' => 'keyword', |
74
|
6 |
|
'boost' => $fluent->boost, |
75
|
6 |
|
'doc_values' => $fluent->doc_values, |
76
|
6 |
|
'eager_global_ordinals' => $fluent->eager_global_ordinals, |
77
|
6 |
|
'fields' => $fluent->fields, |
78
|
6 |
|
'ignore_above' => $fluent->ignore_above, |
79
|
6 |
|
'index' => $fluent->index, |
80
|
6 |
|
'index_options' => $fluent->index_options, |
81
|
6 |
|
'norms' => $fluent->norms, |
82
|
6 |
|
'null_value' => $fluent->null_value, |
83
|
6 |
|
'store' => $fluent->store, |
84
|
6 |
|
'similarity' => $fluent->similarity, |
85
|
6 |
|
'normalizer' => $fluent->normalizer, |
86
|
6 |
|
'split_queries_on_whitespace' => $fluent->split_queries_on_whitespace |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Compile: long |
92
|
|
|
* |
93
|
|
|
* @param FluentLong $fluent |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
2 |
|
public function compileLong(FluentLong $fluent) : array |
97
|
|
|
{ |
98
|
2 |
|
return $this->compileNumeric($fluent); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Compile: integer |
103
|
|
|
* |
104
|
|
|
* @param FluentInteger $fluent |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
4 |
|
public function compileInteger(FluentInteger $fluent) : array |
108
|
|
|
{ |
109
|
4 |
|
return $this->compileNumeric($fluent); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Compile: short |
114
|
|
|
* |
115
|
|
|
* @param FluentShort $fluent |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
2 |
|
public function compileShort(FluentShort $fluent) : array |
119
|
|
|
{ |
120
|
2 |
|
return $this->compileNumeric($fluent); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Compile: byte |
125
|
|
|
* |
126
|
|
|
* @param FluentByte $fluent |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
2 |
|
public function compileByte(FluentByte $fluent) : array |
130
|
|
|
{ |
131
|
2 |
|
return $this->compileNumeric($fluent); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Compile: double |
136
|
|
|
* |
137
|
|
|
* @param FluentDouble $fluent |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
2 |
|
public function compileDouble(FluentDouble $fluent) : array |
141
|
|
|
{ |
142
|
2 |
|
return $this->compileNumeric($fluent); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Compile: float |
147
|
|
|
* |
148
|
|
|
* @param FluentFloat $fluent |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
2 |
|
public function compileFloat(FluentFloat $fluent) : array |
152
|
|
|
{ |
153
|
2 |
|
return $this->compileNumeric($fluent); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Compile: half float |
158
|
|
|
* |
159
|
|
|
* @param FluentHalfFloat $fluent |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
2 |
|
public function compileHalfFloat(FluentHalfFloat $fluent) : array |
163
|
|
|
{ |
164
|
2 |
|
return $this->compileNumeric($fluent); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Compile: scaled float |
169
|
|
|
* |
170
|
|
|
* @param FluentScaledFloat $fluent |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
2 |
|
public function compileScaledFloat(FluentScaledFloat $fluent) : array |
174
|
|
|
{ |
175
|
2 |
|
return $this->compileNumeric($fluent); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Compile: numeric |
180
|
|
|
* |
181
|
|
|
* @param Fluent $fluent |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
11 |
|
public function compileNumeric(Fluent $fluent) : array |
185
|
|
|
{ |
186
|
11 |
|
return $this->formatAttributes([ |
187
|
11 |
|
'type' => $fluent->type, |
188
|
11 |
|
'coerce' => $fluent->coerce, |
189
|
11 |
|
'boost' => $fluent->boost, |
190
|
11 |
|
'doc_values' => $fluent->doc_values, |
191
|
11 |
|
'ignore_malformed' => $fluent->ignore_malformed, |
192
|
11 |
|
'index' => $fluent->index, |
193
|
11 |
|
'null_value' => $fluent->null_value, |
194
|
11 |
|
'store' => $fluent->store |
195
|
|
|
]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Compile: date |
200
|
|
|
* |
201
|
|
|
* @param FluentDate $fluent |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
2 |
|
public function compileDate(FluentDate $fluent) : array |
205
|
|
|
{ |
206
|
2 |
|
return $this->formatAttributes([ |
207
|
2 |
|
'type' => 'date', |
208
|
2 |
|
'boost' => $fluent->boost, |
209
|
2 |
|
'doc_values' => $fluent->doc_values, |
210
|
2 |
|
'format' => $fluent->format, |
211
|
2 |
|
'locale' => $fluent->locale, |
212
|
2 |
|
'ignore_malformed' => $fluent->ignore_malformed, |
213
|
2 |
|
'index' => $fluent->index, |
214
|
2 |
|
'null_value' => $fluent->null_value, |
215
|
2 |
|
'store' => $fluent->store |
216
|
|
|
]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Compile: boolean |
221
|
|
|
* |
222
|
|
|
* @param FluentBoolean $fluent |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
2 |
|
public function compileBoolean(FluentBoolean $fluent) : array |
226
|
|
|
{ |
227
|
2 |
|
return $this->formatAttributes([ |
228
|
2 |
|
'type' => 'boolean', |
229
|
2 |
|
'boost' => $fluent->boost, |
230
|
2 |
|
'doc_values' => $fluent->doc_values, |
231
|
2 |
|
'index' => $fluent->index, |
232
|
2 |
|
'null_value' => $fluent->null_value, |
233
|
2 |
|
'store' => $fluent->store |
234
|
|
|
]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Compile: binary |
239
|
|
|
* |
240
|
|
|
* @param FluentBinary $fluent |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
2 |
|
public function compileBinary(FluentBinary $fluent) : array |
244
|
|
|
{ |
245
|
2 |
|
return $this->formatAttributes([ |
246
|
2 |
|
'type' => 'binary', |
247
|
2 |
|
'doc_values' => $fluent->doc_values, |
248
|
2 |
|
'store' => $fluent->store |
249
|
|
|
]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Compile: integer range |
254
|
|
|
* |
255
|
|
|
* @param FluentIntegerRange $fluent |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
2 |
|
public function compileIntegerRange(FluentIntegerRange $fluent) : array |
259
|
|
|
{ |
260
|
2 |
|
return $this->compileRange($fluent); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Compile: float range |
265
|
|
|
* |
266
|
|
|
* @param FluentFloatRange $fluent |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
2 |
|
public function compileFloatRange(FluentFloatRange $fluent) : array |
270
|
|
|
{ |
271
|
2 |
|
return $this->compileRange($fluent); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Compile: long range |
276
|
|
|
* |
277
|
|
|
* @param FluentLongRange $fluent |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
2 |
|
public function compileLongRange(FluentLongRange $fluent) : array |
281
|
|
|
{ |
282
|
2 |
|
return $this->compileRange($fluent); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Compile: double range |
287
|
|
|
* |
288
|
|
|
* @param FluentDoubleRange $fluent |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
2 |
|
public function compileDoubleRange(FluentDoubleRange $fluent) : array |
292
|
|
|
{ |
293
|
2 |
|
return $this->compileRange($fluent); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Compile: date range |
298
|
|
|
* |
299
|
|
|
* @param FluentDateRange $fluent |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
2 |
|
public function compileDateRange(FluentDateRange $fluent) : array |
303
|
|
|
{ |
304
|
2 |
|
return $this->compileRange($fluent); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Compile: ip range |
309
|
|
|
* |
310
|
|
|
* @param FluentIpRange $fluent |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
2 |
|
public function compileIpRange(FluentIpRange $fluent) : array |
314
|
|
|
{ |
315
|
2 |
|
return $this->compileRange($fluent); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Compile: range |
320
|
|
|
* |
321
|
|
|
* @param Fluent $fluent |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
7 |
|
public function compileRange(Fluent $fluent) : array |
325
|
|
|
{ |
326
|
7 |
|
return $this->formatAttributes([ |
327
|
7 |
|
'type' => $fluent->type, |
328
|
7 |
|
'coerce' => $fluent->coerce, |
329
|
7 |
|
'boost' => $fluent->boost, |
330
|
7 |
|
'index' => $fluent->index, |
331
|
7 |
|
'store' => $fluent->store |
332
|
|
|
]); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Compile: nested |
337
|
|
|
* |
338
|
|
|
* @param FluentNested $fluent |
339
|
|
|
* @return array |
340
|
|
|
*/ |
341
|
2 |
|
public function compileNested(FluentNested $fluent) : array |
342
|
|
|
{ |
343
|
2 |
|
return $this->formatAttributes([ |
344
|
2 |
|
'type' => 'nested', |
345
|
2 |
|
'dynamic' => $fluent->dynamic, |
346
|
2 |
|
'properties' => $this->compileFields( |
347
|
2 |
|
$this->blueprintCallback( |
348
|
2 |
|
$fluent |
349
|
2 |
|
)->getFields() |
350
|
|
|
) |
351
|
|
|
]); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Compile: object |
356
|
|
|
* |
357
|
|
|
* @param FluentObject $fluent |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
2 |
|
public function compileObject(FluentObject $fluent) : array |
361
|
|
|
{ |
362
|
2 |
|
return $this->formatAttributes([ |
363
|
2 |
|
'type' => 'nested', |
364
|
2 |
|
'dynamic' => $fluent->dynamic, |
365
|
2 |
|
'enabled' => $fluent->enabled, |
366
|
2 |
|
'properties' => $this->compileFields( |
367
|
2 |
|
$this->blueprintCallback( |
368
|
2 |
|
$fluent |
369
|
2 |
|
)->getFields() |
370
|
|
|
) |
371
|
|
|
]); |
372
|
|
|
} |
373
|
|
|
|
374
|
3 |
|
private function blueprintCallback(Fluent $fluent) : Blueprint |
375
|
|
|
{ |
376
|
3 |
|
$blueprint = new Blueprint(); |
377
|
|
|
|
378
|
|
|
/** @var \Closure $callback */ |
379
|
3 |
|
$callback = $fluent->callback; |
380
|
|
|
|
381
|
3 |
|
if (is_callable($callback)) { |
382
|
3 |
|
$callback($blueprint); |
383
|
|
|
} |
384
|
|
|
|
385
|
3 |
|
return $blueprint; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Compile: geo point |
390
|
|
|
* |
391
|
|
|
* @param FluentGeoPoint $fluent |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
2 |
|
public function compileGeoPoint(FluentGeoPoint $fluent) : array |
395
|
|
|
{ |
396
|
2 |
|
return $this->formatAttributes([ |
397
|
2 |
|
'type' => 'geo_point', |
398
|
2 |
|
'ignore_malformed' => $fluent->ignore_malformed, |
399
|
2 |
|
'ignore_z_value' => $fluent->ignore_z_value, |
400
|
2 |
|
'null_value' => $fluent->null_value |
401
|
|
|
]); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Compile: geo shape |
406
|
|
|
* |
407
|
|
|
* @param FluentGeoShape $fluent |
408
|
|
|
* @return array |
409
|
|
|
*/ |
410
|
2 |
|
public function compileGeoShape(FluentGeoShape $fluent) : array |
411
|
|
|
{ |
412
|
2 |
|
return $this->formatAttributes([ |
413
|
2 |
|
'type' => 'geo_shape', |
414
|
2 |
|
'tree' => $fluent->tree, |
415
|
2 |
|
'precision' => $fluent->precision, |
416
|
2 |
|
'tree_levels' => $fluent->tree_levels, |
417
|
2 |
|
'strategy' => $fluent->strategy, |
418
|
2 |
|
'distance_error_pct' => $fluent->distance_error_pct, |
419
|
2 |
|
'orientation' => $fluent->orientation, |
420
|
2 |
|
'points_only' => $fluent->points_only, |
421
|
2 |
|
'ignore_malformed' => $fluent->ignore_malformed, |
422
|
2 |
|
'ignore_z_value' => $fluent->ignore_z_value |
423
|
|
|
]); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Compile: ip |
428
|
|
|
* |
429
|
|
|
* @param FluentIp $fluent |
430
|
|
|
* @return array |
431
|
|
|
*/ |
432
|
2 |
|
public function compileIp(FluentIp $fluent) : array |
433
|
|
|
{ |
434
|
2 |
|
return $this->formatAttributes([ |
435
|
2 |
|
'type' => 'ip', |
436
|
2 |
|
'boost' => $fluent->boost, |
437
|
2 |
|
'doc_values' => $fluent->doc_values, |
438
|
2 |
|
'index' => $fluent->index, |
439
|
2 |
|
'null_value' => $fluent->null_value, |
440
|
2 |
|
'store' => $fluent->store |
441
|
|
|
]); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Compile: completion |
446
|
|
|
* |
447
|
|
|
* @param FluentCompletion $fluent |
448
|
|
|
* @return array |
449
|
|
|
*/ |
450
|
2 |
|
public function compileCompletion(FluentCompletion $fluent) : array |
451
|
|
|
{ |
452
|
2 |
|
return $this->formatAttributes([ |
453
|
2 |
|
'type' => 'completion', |
454
|
2 |
|
'analyzer' => $fluent->analyzer, |
455
|
2 |
|
'search_analyzer' => $fluent->search_analyzer, |
456
|
2 |
|
'preserve_separators' => $fluent->preserve_separators, |
457
|
2 |
|
'preserve_position_increments' => $fluent->preserve_position_increments, |
458
|
2 |
|
'max_input_length' => $fluent->max_input_length |
459
|
|
|
]); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Compile: token count |
464
|
|
|
* |
465
|
|
|
* @param FluentTokenCount $fluent |
466
|
|
|
* @return array |
467
|
|
|
*/ |
468
|
2 |
|
public function compileTokenCount(FluentTokenCount $fluent) : array |
469
|
|
|
{ |
470
|
2 |
|
return $this->formatAttributes([ |
471
|
2 |
|
'type' => 'token_count', |
472
|
2 |
|
'analyzer' => $fluent->analyzer, |
473
|
2 |
|
'enable_position_increments' => $fluent->enable_position_increments, |
474
|
2 |
|
'boost' => $fluent->boost, |
475
|
2 |
|
'doc_values' => $fluent->doc_values, |
476
|
2 |
|
'index' => $fluent->index, |
477
|
2 |
|
'null_value' => $fluent->null_value, |
478
|
2 |
|
'store' => $fluent->store |
479
|
|
|
]); |
480
|
|
|
} |
481
|
|
|
|
482
|
30 |
|
private function formatAttributes(array $attributes) : array |
483
|
|
|
{ |
484
|
30 |
|
return array_filter($attributes); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Compile fields |
489
|
|
|
* |
490
|
|
|
* @param array $fields |
491
|
|
|
* @return array |
492
|
|
|
*/ |
493
|
6 |
|
public function compileFields(array $fields) : array |
494
|
|
|
{ |
495
|
6 |
|
$statements = []; |
496
|
|
|
|
497
|
6 |
|
foreach ($fields as $field) { |
498
|
6 |
|
if ($field instanceof Fluent && !is_null($field->type)) { |
499
|
6 |
|
$method = 'compile' . ucfirst(camel_case($field->type)); |
500
|
6 |
|
if (method_exists($this, $method)) { |
501
|
6 |
|
$statements[$field->name] = (array)$this->$method($field); |
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
506
|
6 |
|
return $statements; |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
|