Skip to content

Commit

Permalink
Merge pull request #9709 from nanaya/es-cursor-value
Browse files Browse the repository at this point in the history
Pass elasticsearch sort cursor value as is
  • Loading branch information
notbakaneko authored Sep 1, 2023
2 parents 5ab9b42 + 4c24fd5 commit aed25e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
17 changes: 5 additions & 12 deletions app/Libraries/Elasticsearch/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,17 @@ public function getPaginator(array $options = [])

public function getSortCursor(): ?array
{
// FIXME: should cast cursor values to match sort.
$requested = $this->params->size;
$received = $this->response()->count();
$total = $this->response()->total();

if ($received === $requested && $received < $total) {
$last = array_last($this->response()->hits());
if (array_key_exists('sort', $last)) {
$fields = array_map(function ($sort) {
return $sort->field;
}, $this->params->sorts);

$casted = array_map(function ($value) {
// stringify all ints since javascript doesn't like big ints.
// fortunately the minimum value is PHP_INT_MIN instead of the equivalent double.
return is_int($value) ? (string) $value : $value;
}, $last['sort']);

return array_combine($fields, $casted);
return array_combine(
array_map(fn ($sort) => $sort->field, $this->params->sorts),
$last['sort'],
);
}
}

Expand Down Expand Up @@ -193,6 +185,7 @@ public function response(): SearchResponse
*/
public function searchAfter(?array $searchAfter)
{
// FIXME: The values should be sanitised. The count and type must match sort options.
$this->params->searchAfter = $searchAfter;
$this->response = null;

Expand Down
2 changes: 0 additions & 2 deletions app/Libraries/Score/FetchDedupedScores.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public function all(): array

while ($hasNext) {
if ($nextCursor !== null) {
// FIXME: cursor value returned is 0/1 but elasticsearch expects false/true...
$nextCursor['is_legacy'] = get_bool($nextCursor['is_legacy']);
$search->searchAfter(array_values($nextCursor));
}
$response = $search->response();
Expand Down

0 comments on commit aed25e4

Please sign in to comment.