Skip to content

Commit 414817f

Browse files
committed
extended sorting parameters
1 parent 89dfb17 commit 414817f

File tree

5 files changed

+77
-27
lines changed

5 files changed

+77
-27
lines changed

src/DSL/Bridge.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ public function processDistinct($wheres, $options, $columns, $includeDocCount =
141141
unset($options['skip']);
142142
unset($options['limit']);
143143

144+
if ($sort) {
145+
$sortField = key($sort);
146+
$sortDir = $sort[$sortField]['order'] ?? 'asc';
147+
$sort = [$sortField => $sortDir];
148+
}
149+
150+
144151
$params = $this->buildParams($this->index, $wheres, $options);
145152
$params['body']['aggs'] = $this->createNestedAggs($columns, $sort);
146153

@@ -821,9 +828,11 @@ private function _queryTag($function)
821828

822829
private function _sanitizeSearchResponse($response, $params, $queryTag)
823830
{
831+
824832
$meta['timed_out'] = $response['timed_out'];
825833
$meta['total'] = $response['hits']['total']['value'] ?? 0;
826834
$meta['max_score'] = $response['hits']['max_score'] ?? 0;
835+
$meta['sorts'] = [];
827836
$data = [];
828837
if (!empty($response['hits']['hits'])) {
829838
foreach ($response['hits']['hits'] as $hit) {
@@ -835,13 +844,30 @@ private function _sanitizeSearchResponse($response, $params, $queryTag)
835844
$datum[$key] = $value;
836845
}
837846
}
847+
//------------------------ later, maybe ------------------------------
848+
// if (!empty($hit['sort'])) {
849+
// $datum['_meta']['sort'] = $this->_parseSort($hit['sort'], $params['body']['sort'] ?? []);
850+
// }
851+
//----------------------------------------------------------------------
852+
853+
838854
$data[] = $datum;
839855
}
840856
}
841857

842858
return $this->_return($data, $meta, $params, $queryTag);
843859
}
844860

861+
private function _parseSort($sort, $sortParams)
862+
{
863+
$sortValues = [];
864+
foreach ($sort as $key => $value) {
865+
$sortValues[array_key_first($sortParams[$key])] = $value;
866+
}
867+
868+
return $sortValues;
869+
}
870+
845871
private function _sanitizeDistinctResponse($response, $columns, $includeDocCount)
846872
{
847873
$keys = [];

src/DSL/ParameterBuilder.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,24 @@ public static function query($dsl): array
3333
}
3434

3535

36-
public static function fieldSort($field, $order = 'asc'): array
36+
public static function fieldSort($field, $payload): array
3737
{
38+
39+
$sort = [];
40+
$sort['order'] = $payload['order'] ?? 'asc';
41+
if (!empty($payload['mode'])) {
42+
$sort['mode'] = $payload['mode'];
43+
}
44+
if (!empty($payload['missing'])) {
45+
$sort['missing'] = $payload['missing'];
46+
}
47+
3848
return [
39-
$field => [
40-
'order' => $order,
41-
],
49+
$field => $sort,
4250
];
4351
}
4452

53+
4554
public static function maxAggregation($field): array
4655
{
4756
return [

src/DSL/QueryBuilder.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ public function createNestedAggs($columns, $sort)
129129
if (!isset($terms['terms']['order'])) {
130130
$terms['terms']['order'] = [];
131131
}
132-
if ($sort['_count'] == 1) {
132+
if ($sort['_count'] == 'asc') {
133133
$terms['terms']['order'][] = ['_count' => 'asc'];
134134
} else {
135135
$terms['terms']['order'][] = ['_count' => 'desc'];
136136
}
137137
}
138138
if (isset($sort[$columns[0]])) {
139-
if ($sort[$columns[0]] == 1) {
139+
if ($sort[$columns[0]] == 'asc') {
140140
$terms['terms']['order'][] = ['_key' => 'asc'];
141141
} else {
142142
$terms['terms']['order'][] = ['_key' => 'desc'];
@@ -205,15 +205,15 @@ private function _buildQuery($wheres): array
205205
}
206206

207207

208-
public function _convertWheresToDSL($wheres): array
208+
public function _convertWheresToDSL($wheres, $parentField = false): array
209209
{
210210
$dsl = ['bool' => []];
211211
foreach ($wheres as $logicalOperator => $conditions) {
212212
switch ($logicalOperator) {
213213
case 'and':
214214
$dsl['bool']['must'] = [];
215215
foreach ($conditions as $condition) {
216-
$parsedCondition = $this->_parseCondition($condition);
216+
$parsedCondition = $this->_parseCondition($condition, $parentField);
217217
if (!empty($parsedCondition)) {
218218
$dsl['bool']['must'][] = $parsedCondition;
219219
}
@@ -225,7 +225,7 @@ public function _convertWheresToDSL($wheres): array
225225
$boolClause = ['bool' => ['must' => []]];
226226
foreach ($conditionGroup as $subConditions) {
227227
foreach ($subConditions as $subCondition) {
228-
$parsedCondition = $this->_parseCondition($subCondition);
228+
$parsedCondition = $this->_parseCondition($subCondition, $parentField);
229229
if (!empty($parsedCondition)) {
230230
$boolClause['bool']['must'][] = $parsedCondition;
231231
}
@@ -237,17 +237,22 @@ public function _convertWheresToDSL($wheres): array
237237
}
238238
break;
239239
default:
240-
return $this->_parseCondition($wheres);
240+
return $this->_parseCondition($wheres, $parentField);
241241
}
242242
}
243243

244244
return $dsl;
245245
}
246246

247-
private function _parseCondition($condition): array
247+
private function _parseCondition($condition, $parentField = null): array
248248
{
249-
// dd($condition);
250249
$field = key($condition);
250+
if ($parentField) {
251+
if (!str_starts_with($field, $parentField.'.')) {
252+
$field = $parentField.'.'.$field;
253+
}
254+
}
255+
251256
$value = current($condition);
252257

253258

@@ -345,7 +350,7 @@ private function _parseCondition($condition): array
345350
$queryPart = [
346351
'nested' => [
347352
'path' => $field,
348-
'query' => $this->_convertWheresToDSL($operand['wheres']),
353+
'query' => $this->_convertWheresToDSL($operand['wheres'], $field),
349354
'score_mode' => $operand['score_mode'],
350355
],
351356
];
@@ -390,8 +395,8 @@ private function _buildOptions($options): array
390395
if (!isset($return['body']['sort'])) {
391396
$return['body']['sort'] = [];
392397
}
393-
foreach ($value as $field => $direction) {
394-
$return['body']['sort'][] = $this->_parseSortOrder($field, $direction);
398+
foreach ($value as $field => $sortPayload) {
399+
$return['body']['sort'][] = ParameterBuilder::fieldSort($field, $sortPayload);
395400
}
396401
break;
397402
case 'skip':
@@ -405,8 +410,10 @@ private function _buildOptions($options): array
405410
$this->_parseFilter($filterType, $filerValues);
406411
}
407412
break;
413+
408414
case 'multiple':
409415
case 'searchOptions':
416+
410417
//Pass through
411418
break;
412419
default:
@@ -440,15 +447,6 @@ public function _parseFilter($filterType, $filterPayload): void
440447
}
441448
}
442449

443-
private function _parseSortOrder($field, $direction): array
444-
{
445-
$dir = 'desc';
446-
if ($direction == 1) {
447-
$dir = 'asc';
448-
}
449-
450-
return ParameterBuilder::fieldSort($field, $dir);
451-
}
452450

453451
public function _parseFilterParameter($params, $filer)
454452
{

src/Eloquent/Docs/ModelDocs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* @method $this whereNotNestedObject(string $column, Callable $callback, string $scoreType = 'avg')
3030
* @method $this firstOrCreate(array $attributes, array $values = [])
3131
* @method $this firstOrCreateWithoutRefresh(array $attributes, array $values = [])
32+
* @method $this orderBy(string $column, string $direction = 'asc', string $mode = null, array $missing = '_last')
33+
* @method $this orderByDesc(string $column, string $mode = null, array $missing = '_last')
3234
* @method $this deleteIndexIfExists()
3335
*
3436
*

src/Query/Builder.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,17 +519,31 @@ protected function _processDelete()
519519
/**
520520
* @inheritdoc
521521
*/
522-
public function orderBy($column, $direction = 'asc')
522+
public function orderBy($column, $direction = 'asc', $mode = null, $missing = null)
523523
{
524524
if (is_string($direction)) {
525-
$direction = (strtolower($direction) == 'asc' ? 1 : -1);
525+
$direction = (strtolower($direction) == 'asc' ? 'asc' : 'desc');
526526
}
527527

528-
$this->orders[$column] = $direction;
528+
$this->orders[$column] = [
529+
'order' => $direction,
530+
'mode' => $mode,
531+
'missing' => $missing,
532+
];
533+
534+
// dd($this->orders);
529535

530536
return $this;
531537
}
532538

539+
/**
540+
* @inheritDoc
541+
*/
542+
public function orderByDesc($column, $mode = null, $missing = null)
543+
{
544+
return $this->orderBy($column, 'desc', $mode, $missing);
545+
}
546+
533547
/**
534548
* @inheritdoc
535549
*/
@@ -1253,6 +1267,7 @@ public function matrix($column)
12531267
// ES Search query methods
12541268
//----------------------------------------------------------------------
12551269

1270+
12561271
public function searchQuery($term, $boostFactor = null, $clause = null, $type = 'term')
12571272
{
12581273
if (!$clause && !empty($this->searchQuery)) {

0 commit comments

Comments
 (0)