diff --git a/src/search.rs b/src/search.rs index cedca1085..e7100e148 100644 --- a/src/search.rs +++ b/src/search.rs @@ -347,25 +347,12 @@ impl<'a> Search<'a> { .facet_field_id_value_docids .remap_key_type::(); - let biggest_level = match fid.checked_add(1) { - Some(next_fid) => { - // If we are able to find the next field id we ask the key that is before - // the first entry of it which corresponds to the last key of our field id. - let db = db.remap_data_type::(); - match db.get_lower_than(self.rtxn, &(next_fid, 0, i64::MIN, i64::MIN))? { - Some(((id, level, ..), _)) if fid == id => Some(level), - _ => None, - } - }, - None => { - // If we can't generate a bigger field id, it must be equal to 255 and - // therefore the last key of the database must be the one we want. - match db.remap_data_type::().last(self.rtxn)? { - Some(((id, level, ..), _)) if fid == id => Some(level), - _ => None, - } - }, - }; + // Ask for the biggest value that can exist for this specific field, if it exists + // that's fine if it don't, the value just before will be returned instead. + let biggest_level = db + .remap_data_type::() + .get_lower_than_or_equal_to(self.rtxn, &(fid, u8::MAX, i64::MAX, i64::MAX))? + .and_then(|((id, level, _, _), _)| if id == fid { Some(level) } else { None }); match biggest_level { Some(level) => {