Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 3b8e570

Browse files
AidynMakhataevAidyn Makhataev
andauthored
Added orderRaw (#370)
* add orderRaw method * add test for orderRaw method * add Readme for orderRaw * add readme for sortPayload * styleci fix * styleci fix Co-authored-by: Aidyn Makhataev <[email protected]>
1 parent 39a550f commit 3b8e570

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,28 @@ App\MyModel::search('sales')
274274
->get();
275275
```
276276

277+
And add more complex sorting (geo_distance eg.)
278+
279+
```php
280+
$model = App\MyModel::search('sales')
281+
->orderRaw([
282+
'_geo_distance' => [
283+
'coordinates' => [
284+
'lat' => 51.507351,
285+
'lon' => -0.127758
286+
],
287+
'order' => 'asc',
288+
'unit' => 'm'
289+
]
290+
])
291+
->get();
292+
293+
// To retrieve sort result, use model `sortPayload` attribute:
294+
$model->sortPayload;
295+
```
296+
297+
298+
277299
At last, if you want to send a custom request, you can use the `searchRaw` method:
278300

279301
```php

src/Builders/FilterBuilder.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ public function orderBy($field, $direction = 'asc')
446446
return $this;
447447
}
448448

449+
/**
450+
* Add a raw order clause.
451+
*
452+
* @param array $payload
453+
* @return $this
454+
*/
455+
public function orderRaw(array $payload)
456+
{
457+
$this->orders[] = $payload;
458+
459+
return $this;
460+
}
461+
449462
/**
450463
* Explain the request.
451464
*

src/ElasticEngine.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ public function map(Builder $builder, $results, $model)
326326
$model->highlight = new Highlight($hit['highlight']);
327327
}
328328

329+
//add sort information to results for use
330+
if (isset($hit['sort'])) {
331+
$model->sortPayload = $hit['sort'];
332+
}
333+
329334
return $model;
330335
}
331336
})

tests/Builders/FilterBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,25 @@ public function testOrderBy()
424424
);
425425
}
426426

427+
public function testOrderRaw()
428+
{
429+
$orderRaw = [
430+
'_geo_distance' => [
431+
'coordinates' => [
432+
'lat' => 51.507351,
433+
'lon' => -0.127758,
434+
],
435+
'order' => 'asc',
436+
'unit' => 'm',
437+
],
438+
];
439+
440+
$builder = (new FilterBuilder($this->mockModel()))
441+
->orderRaw($orderRaw);
442+
443+
$this->assertSame([$orderRaw], $builder->orders);
444+
}
445+
427446
public function testWith()
428447
{
429448
$builder = (new FilterBuilder($this->mockModel()))

0 commit comments

Comments
 (0)