@@ -30,14 +30,18 @@ class SortBy implements HttpTransportable
30
30
*
31
31
* Sort by score
32
32
*/
33
- const SCORE = ['_score ' => ['order ' => self ::DESC ]];
33
+ const SCORE = [
34
+ 'type ' => self ::TYPE_SCORE ,
35
+ ];
34
36
35
37
/**
36
38
* @var array
37
39
*
38
40
* Sort random
39
41
*/
40
- const RANDOM = ['random ' => true ];
42
+ const RANDOM = [
43
+ 'type ' => self ::TYPE_RANDOM ,
44
+ ];
41
45
42
46
/**
43
47
* @var array
@@ -51,69 +55,102 @@ class SortBy implements HttpTransportable
51
55
*
52
56
* Sort by id ASC
53
57
*/
54
- const ID_ASC = ['uuid.id ' => ['order ' => self ::ASC ]];
58
+ const ID_ASC = [
59
+ 'field ' => 'uuid.id ' ,
60
+ 'order ' => self ::ASC ,
61
+ ];
55
62
56
63
/**
57
64
* @var array
58
65
*
59
66
* Sort by id DESC
60
67
*/
61
- const ID_DESC = ['uuid.id ' => ['order ' => self ::DESC ]];
68
+ const ID_DESC = [
69
+ 'field ' => 'uuid.id ' ,
70
+ 'order ' => self ::DESC ,
71
+ ];
62
72
63
73
/**
64
74
* @var array
65
75
*
66
76
* Sort by type ASC
67
77
*/
68
- const TYPE_ASC = ['uuid.type ' => ['order ' => self ::ASC ]];
78
+ const TYPE_ASC = [
79
+ 'field ' => 'uuid.type ' ,
80
+ 'order ' => self ::ASC ,
81
+ ];
69
82
70
83
/**
71
84
* @var array
72
85
*
73
86
* Sort by type DESC
74
87
*/
75
- const TYPE_DESC = ['uuid.type ' => ['order ' => self ::DESC ]];
88
+ const TYPE_DESC = [
89
+ 'field ' => 'uuid.type ' ,
90
+ 'order ' => self ::DESC ,
91
+ ];
76
92
77
93
/**
78
94
* @var array
79
95
*
80
96
* Sort by location ASC using KM
81
97
*/
82
- const LOCATION_KM_ASC = ['_geo_distance ' => ['order ' => self ::ASC , 'unit ' => 'km ' ]];
98
+ const LOCATION_KM_ASC = [
99
+ 'type ' => self ::TYPE_DISTANCE ,
100
+ 'unit ' => 'km ' ,
101
+ ];
83
102
84
103
/**
85
104
* @var array
86
105
*
87
106
* Sort by location ASC using Miles
88
107
*/
89
- const LOCATION_MI_ASC = ['_geo_distance ' => ['order ' => self ::ASC , 'unit ' => 'mi ' ]];
108
+ const LOCATION_MI_ASC = [
109
+ 'type ' => self ::TYPE_DISTANCE ,
110
+ 'unit ' => 'mi ' ,
111
+ ];
112
+
90
113
/**
91
- * @var array
114
+ * @var string
92
115
*
93
- * Sort by id ASC
116
+ * Type field
94
117
*/
95
- const OCCURRED_ON_ASC = [ ' indexed_metadata.occurred_on ' => [ ' order ' => self :: ASC ]] ;
118
+ const TYPE_FIELD = ' field ' ;
96
119
97
120
/**
98
- * @var array
121
+ * @var string
99
122
*
100
- * Sort by id DESC
123
+ * Type score
101
124
*/
102
- const OCCURRED_ON_DESC = [ ' indexed_metadata.occurred_on ' => [ ' order ' => self :: DESC ]] ;
125
+ const TYPE_SCORE = ' score ' ;
103
126
104
127
/**
105
- * @var int
128
+ * @var string
106
129
*
107
- * Type field
130
+ * Type random
108
131
*/
109
- const TYPE_FIELD = 1 ;
132
+ const TYPE_RANDOM = ' random ' ;
110
133
111
134
/**
112
- * @var int
135
+ * @var string
136
+ *
137
+ * Type distance
138
+ */
139
+ const TYPE_DISTANCE = 'distance ' ;
140
+
141
+ /**
142
+ * @var string
113
143
*
114
144
* Type nested
115
145
*/
116
- const TYPE_NESTED = 2 ;
146
+ const TYPE_NESTED = 'nested ' ;
147
+
148
+ /**
149
+ * @var string
150
+ *
151
+ * Type function
152
+ */
153
+ const TYPE_FUNCTION = 'function ' ;
117
154
118
155
/**
119
156
* @var string
@@ -250,9 +287,8 @@ public function byFieldValue(
250
287
): SortBy {
251
288
$ this ->sortsBy [] = [
252
289
'type ' => self ::TYPE_FIELD ,
253
- Item::getPathByField ($ field ) => [
254
- 'order ' => $ order ,
255
- ],
290
+ 'field ' => Item::getPathByField ($ field ),
291
+ 'order ' => $ order ,
256
292
];
257
293
258
294
return $ this ;
@@ -275,9 +311,8 @@ public function byNestedField(
275
311
$ this ->sortsBy [] = [
276
312
'type ' => self ::TYPE_NESTED ,
277
313
'mode ' => $ mode ,
278
- "indexed_metadata. $ field " => [
279
- 'order ' => $ order ,
280
- ],
314
+ 'field ' => "indexed_metadata. $ field " ,
315
+ 'order ' => $ order ,
281
316
];
282
317
283
318
return $ this ;
@@ -307,9 +342,29 @@ public function byNestedFieldAndFilter(
307
342
'type ' => self ::TYPE_NESTED ,
308
343
'filter ' => $ filter ,
309
344
'mode ' => $ mode ,
310
- "indexed_metadata. $ field " => [
311
- 'order ' => $ order ,
312
- ],
345
+ 'field ' => "indexed_metadata. $ field " ,
346
+ 'order ' => $ order ,
347
+ ];
348
+
349
+ return $ this ;
350
+ }
351
+
352
+ /**
353
+ * Sort by function.
354
+ *
355
+ * @param string $function
356
+ * @param string $order
357
+ *
358
+ * @return SortBy
359
+ */
360
+ public function byFunction (
361
+ string $ function ,
362
+ string $ order
363
+ ): SortBy {
364
+ $ this ->sortsBy [] = [
365
+ 'type ' => self ::TYPE_FUNCTION ,
366
+ 'function ' => $ function ,
367
+ 'order ' => $ order ,
313
368
];
314
369
315
370
return $ this ;
@@ -323,7 +378,7 @@ public function byNestedFieldAndFilter(
323
378
public function isSortedByGeoDistance (): bool
324
379
{
325
380
foreach ($ this ->sortsBy as $ sortBy ) {
326
- if (isset ( $ sortBy ['_geo_distance ' ]) ) {
381
+ if (self :: TYPE_DISTANCE === $ sortBy ['type ' ] ) {
327
382
return true ;
328
383
}
329
384
}
@@ -341,8 +396,8 @@ public function isSortedByGeoDistance(): bool
341
396
public function setCoordinate (Coordinate $ coordinate )
342
397
{
343
398
$ this ->sortsBy = array_map (function (array $ sortBy ) use ($ coordinate ) {
344
- if (isset ( $ sortBy ['_geo_distance ' ]) ) {
345
- $ sortBy ['_geo_distance ' ][ ' coordinate ' ] = $ coordinate ;
399
+ if (self :: TYPE_DISTANCE === $ sortBy ['type ' ] ) {
400
+ $ sortBy ['coordinate ' ] = $ coordinate ;
346
401
}
347
402
348
403
return $ sortBy ;
@@ -359,13 +414,6 @@ public function setCoordinate(Coordinate $coordinate)
359
414
public function toArray (): array
360
415
{
361
416
return array_map (function (array $ sortBy ) {
362
- if (
363
- isset ($ sortBy ['type ' ]) &&
364
- self ::TYPE_FIELD == $ sortBy ['type ' ]
365
- ) {
366
- unset($ sortBy ['type ' ]);
367
- }
368
-
369
417
if (
370
418
isset ($ sortBy ['filter ' ]) &&
371
419
($ sortBy ['filter ' ] instanceof Filter)
@@ -374,10 +422,10 @@ public function toArray(): array
374
422
}
375
423
376
424
if (
377
- isset ($ sortBy ['_geo_distance ' ]) &&
378
- isset ( $ sortBy ['_geo_distance ' ][ ' coordinate ' ]) &&
379
- ( $ sortBy [ ' _geo_distance ' ][ ' coordinate ' ] instanceof Coordinate) ) {
380
- $ sortBy ['_geo_distance ' ][ ' coordinate ' ] = $ sortBy[ ' _geo_distance ' ] ['coordinate ' ]->toArray ();
425
+ isset ($ sortBy ['coordinate ' ]) &&
426
+ $ sortBy ['coordinate ' ] instanceof Coordinate
427
+ ) {
428
+ $ sortBy ['coordinate ' ] = $ sortBy ['coordinate ' ]->toArray ();
381
429
}
382
430
383
431
return $ sortBy ;
@@ -409,10 +457,10 @@ public static function createFromArray(array $array)
409
457
}
410
458
411
459
if (
412
- isset ($ element ['_geo_distance ' ]) &&
413
- isset ($ element ['_geo_distance ' ][ ' coordinate ' ]) &&
414
- is_array ( $ element [ ' _geo_distance ' ][ ' coordinate ' ]) ) {
415
- $ element ['_geo_distance ' ][ ' coordinate ' ] = Coordinate::createFromArray ($ element[ ' _geo_distance ' ] ['coordinate ' ]);
460
+ isset ($ element ['coordinate ' ]) &&
461
+ is_array ($ element ['coordinate ' ])
462
+ ) {
463
+ $ element ['coordinate ' ] = Coordinate::createFromArray ($ element ['coordinate ' ]);
416
464
}
417
465
418
466
return $ element ;
@@ -429,7 +477,7 @@ public static function createFromArray(array $array)
429
477
public function hasRandomSort ()
430
478
{
431
479
foreach ($ this ->sortsBy as $ sortBy ) {
432
- if (self ::RANDOM === $ sortBy ) {
480
+ if (self ::TYPE_RANDOM === $ sortBy[ ' type ' ] ) {
433
481
return true ;
434
482
}
435
483
}
0 commit comments