-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Sorting | ||
|
||
By default, your search results will be sorted by their score according to Elasticsearch. | ||
If you want to step in and influence the sorting you may do so using a simplified implementation in Explorer. | ||
Currently it is only possible to define one sort order. | ||
|
||
```php | ||
use App\Models\Post; | ||
use \JeroenG\Explorer\Domain\Syntax\Sort; | ||
|
||
$results = Post::search('Self-steering') | ||
->sort(new Sort('published_at')) | ||
->get(); | ||
``` | ||
|
||
The first parameter of a `Sort()` object is the name of the field, an optional second parameter is for the order. | ||
|
||
```php | ||
use \JeroenG\Explorer\Domain\Syntax\Sort; | ||
|
||
new Sort('id', Sort::ASCENDING); // the default | ||
new Sort('id', Sort::DESCENDING); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Syntax; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
class Sort implements SyntaxInterface | ||
{ | ||
public const ASCENDING = 'asc'; | ||
|
||
public const DESCENDING = 'desc'; | ||
|
||
private string $field; | ||
|
||
private string $order; | ||
|
||
public function __construct(string $field, string $order = self::ASCENDING) | ||
{ | ||
$this->field = $field; | ||
$this->order = $order; | ||
Assert::inArray($order, [self::ASCENDING, self::DESCENDING]); | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [$this->field => $this->order]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters