Change page and hitsPerPage corner cases

This commit is contained in:
ManyTheFish 2022-07-21 17:42:42 +02:00
parent 815fba9cc3
commit dfa70e47f7

View File

@ -1,4 +1,4 @@
use std::cmp::{max, min}; use std::cmp::min;
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr; use std::str::FromStr;
use std::time::Instant; use std::time::Instant;
@ -152,12 +152,11 @@ impl Index {
search.exhaustive_number_hits(is_finite_pagination); search.exhaustive_number_hits(is_finite_pagination);
let (offset, limit) = if is_finite_pagination { let (offset, limit) = if is_finite_pagination {
// we start at least at page 1. let offset = min(
let page = max(query.page, 1); query.hits_per_page * (query.page.saturating_sub(1)),
// return at least 1 document. max_total_hits,
let hits_per_page = max(query.hits_per_page, 1); );
let offset = min(hits_per_page * (page - 1), max_total_hits); let limit = min(query.hits_per_page, max_total_hits.saturating_sub(offset));
let limit = min(hits_per_page, max_total_hits.saturating_sub(offset));
(offset, limit) (offset, limit)
} else { } else {
@ -295,12 +294,15 @@ impl Index {
let number_of_hits = min(candidates.len() as usize, max_total_hits); let number_of_hits = min(candidates.len() as usize, max_total_hits);
let hits_info = if is_finite_pagination { let hits_info = if is_finite_pagination {
// return at least 1 document. // If hit_per_page is 0, then pages can't be computed and so we respond 0.
let hits_per_page = max(query.hits_per_page, 1); let total_pages = (number_of_hits + query.hits_per_page.saturating_sub(1))
.checked_div(query.hits_per_page)
.unwrap_or(0);
HitsInfo::Pagination { HitsInfo::Pagination {
hits_per_page, hits_per_page: query.hits_per_page,
page: offset / hits_per_page + 1, page: query.page,
total_pages: (number_of_hits + hits_per_page - 1) / hits_per_page, total_pages,
total_hits: number_of_hits, total_hits: number_of_hits,
} }
} else { } else {