Skip to content

Commit b1bf7b5

Browse files
committed
fix(sort): added ability to add score sorting to sort collection
- Changed properties in Sort class from private to public - Enhanced handling of '_score' field in SortCollection and Options classes
1 parent 04573e3 commit b1bf7b5

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

src/Options.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public function toArray(): array
8484
}
8585

8686
foreach ($this->sort as $item) {
87+
if ($item->field === '_score') {
88+
$array['sort'][] = $item->field;
89+
continue;
90+
}
91+
8792
$array['sort'][] = $item->toArray();
8893
}
8994

src/Options/Sort.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Sort implements \Spameri\ElasticQuery\Entity\EntityInterface
1717
public const MISSING_FIRST = '_first';
1818

1919
public function __construct(
20-
private string $field,
21-
private string $type = self::DESC,
22-
private string $missing = self::MISSING_LAST,
20+
public string $field,
21+
public string $type = self::DESC,
22+
public string $missing = self::MISSING_LAST,
2323
)
2424
{
2525
if ( ! \in_array($type, [self::ASC, self::DESC], true)) {

src/Options/SortCollection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ public function toArray(): array
1212
{
1313
$array = [];
1414

15+
/** @var \Spameri\ElasticQuery\Options\Sort $sort */
1516
foreach ($this->collection as $sort) {
17+
if ($sort->field === '_score') {
18+
$array[] = $sort->field;
19+
continue;
20+
}
21+
1622
$array[] = $sort->toArray();
1723
}
1824

0 commit comments

Comments
 (0)