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

Commit

Permalink
Added orderRaw (#370)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
AidynMakhataev and Aidyn Makhataev authored Jun 1, 2020
1 parent 39a550f commit 3b8e570
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,28 @@ App\MyModel::search('sales')
->get();
```

And add more complex sorting (geo_distance eg.)

```php
$model = App\MyModel::search('sales')
->orderRaw([
'_geo_distance' => [
'coordinates' => [
'lat' => 51.507351,
'lon' => -0.127758
],
'order' => 'asc',
'unit' => 'm'
]
])
->get();

// To retrieve sort result, use model `sortPayload` attribute:
$model->sortPayload;
```



At last, if you want to send a custom request, you can use the `searchRaw` method:

```php
Expand Down
13 changes: 13 additions & 0 deletions src/Builders/FilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,19 @@ public function orderBy($field, $direction = 'asc')
return $this;
}

/**
* Add a raw order clause.
*
* @param array $payload
* @return $this
*/
public function orderRaw(array $payload)
{
$this->orders[] = $payload;

return $this;
}

/**
* Explain the request.
*
Expand Down
5 changes: 5 additions & 0 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public function map(Builder $builder, $results, $model)
$model->highlight = new Highlight($hit['highlight']);
}

//add sort information to results for use
if (isset($hit['sort'])) {
$model->sortPayload = $hit['sort'];
}

return $model;
}
})
Expand Down
19 changes: 19 additions & 0 deletions tests/Builders/FilterBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,25 @@ public function testOrderBy()
);
}

public function testOrderRaw()
{
$orderRaw = [
'_geo_distance' => [
'coordinates' => [
'lat' => 51.507351,
'lon' => -0.127758,
],
'order' => 'asc',
'unit' => 'm',
],
];

$builder = (new FilterBuilder($this->mockModel()))
->orderRaw($orderRaw);

$this->assertSame([$orderRaw], $builder->orders);
}

public function testWith()
{
$builder = (new FilterBuilder($this->mockModel()))
Expand Down

0 comments on commit 3b8e570

Please sign in to comment.