Skip to content

Commit b5e8325

Browse files
committed
Update result sorting
1 parent db722c4 commit b5e8325

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/Services/Search/Drivers/ElasticSearchDriver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,7 @@ public function searchReleasesFiltered(array $criteria, int $limit, int $offset
29112911
'query' => $query,
29122912
'sort' => [
29132913
[$sortField => ['order' => $order]],
2914+
['id' => ['order' => $order]],
29142915
],
29152916
'from' => max(0, $offset),
29162917
'size' => max(1, min($limit, self::MAX_RESULTS)),

app/Services/Search/Drivers/ManticoreSearchDriver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,12 @@ public function searchReleasesFiltered(array $criteria, int $limit, int $offset
24302430
if (! in_array($sortField, $allowedSort, true)) {
24312431
$sortField = 'postdate_ts';
24322432
}
2433-
$query->sort($sortField, $sortDir === 'asc' ? 'asc' : 'desc');
2433+
$order = $sortDir === 'asc' ? 'asc' : 'desc';
2434+
$query->sort($sortField, $order);
2435+
if ($sortField !== 'id') {
2436+
// Stabilize ordering across pages when primary sort values are equal.
2437+
$query->sort('id', $order);
2438+
}
24342439

24352440
$maxNeed = max(1000, $offset + $limit + 100);
24362441
$query->maxMatches(min($maxNeed, (int) ($this->config['max_matches'] ?? 10000)));

tests/Unit/ManticoreSearchQueryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,22 @@ public function it_clamps_search_limit_to_configured_max_matches(): void
207207
$this->assertSame(500, $method->invoke($driver, 750));
208208
$this->assertSame(1, $method->invoke($driver, 0));
209209
}
210+
211+
#[Test]
212+
public function search_releases_filtered_uses_stable_id_tiebreak_sort_for_manticore(): void
213+
{
214+
$driverSource = file_get_contents(__DIR__.'/../../app/Services/Search/Drivers/ManticoreSearchDriver.php');
215+
216+
$this->assertIsString($driverSource);
217+
$this->assertStringContainsString("\$query->sort('id', \$order);", $driverSource);
218+
}
219+
220+
#[Test]
221+
public function search_releases_filtered_uses_stable_id_tiebreak_sort_for_elasticsearch(): void
222+
{
223+
$driverSource = file_get_contents(__DIR__.'/../../app/Services/Search/Drivers/ElasticSearchDriver.php');
224+
225+
$this->assertIsString($driverSource);
226+
$this->assertStringContainsString("['id' => ['order' => \$order]],", $driverSource);
227+
}
210228
}

0 commit comments

Comments
 (0)